Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.37% covered (danger)
34.37%
988 / 2875
5.45% covered (danger)
5.45%
3 / 55
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppController
34.32% covered (danger)
34.32%
986 / 2873
5.45% covered (danger)
5.45%
3 / 55
265915.69
0.00% covered (danger)
0.00%
0 / 1
 processSGF
73.58% covered (warning)
73.58%
39 / 53
0.00% covered (danger)
0.00%
0 / 1
19.15
 xFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 yFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getInitialPositionEnd
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
3.47
 getInitialPosition
50.00% covered (danger)
50.00%
10 / 20
0.00% covered (danger)
0.00%
0 / 1
10.50
 getInvisibleSets
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getDeletedSets
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 startPageUpdate
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
 uotd
0.00% covered (danger)
0.00%
0 / 157
0.00% covered (danger)
0.00%
0 / 1
2070
 deleteUnusedStatuses
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
72
 saveSolvedNumber
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
156
 convertEloToXp
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 resetUserElos
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 getNewElo
58.23% covered (warning)
58.23%
46 / 79
0.00% covered (danger)
0.00%
0 / 1
57.28
 handleContribution
52.94% covered (warning)
52.94%
9 / 17
0.00% covered (danger)
0.00%
0 / 1
2.42
 getAllTags
96.55% covered (success)
96.55%
28 / 29
0.00% covered (danger)
0.00%
0 / 1
7
 userRefresh
0.00% covered (danger)
0.00%
0 / 146
0.00% covered (danger)
0.00%
0 / 1
992
 deleteUserBoards
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 halfXP
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 getNewTsumego
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 publishSingle
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
30
 getTsumegoOfTheDay
0.00% covered (danger)
0.00%
0 / 104
0.00% covered (danger)
0.00%
0 / 1
1190
 ratingMatch
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
992
 rating2
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
132
 encrypt
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 decrypt
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 checkPictureLarge
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 checkPicture
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 getTsumegoRankx
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
506
 adjustElo
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
132
 getTsumegoElo
60.38% covered (warning)
60.38%
32 / 53
0.00% covered (danger)
0.00%
0 / 1
95.70
 getTsumegoRankVal
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
506
 getTsumegoRankMax
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
506
 saveDanSolveCondition
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
7
 updateSprintCondition
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
4.00
 updateGoldenCondition
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
3.00
 setPotionCondition
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 updateGems
3.85% covered (danger)
3.85%
4 / 104
0.00% covered (danger)
0.00%
0 / 1
2183.48
 checkProblemNumberAchievements
42.39% covered (danger)
42.39%
39 / 92
0.00% covered (danger)
0.00%
0 / 1
166.38
 checkDanSolveAchievements
54.91% covered (warning)
54.91%
95 / 173
0.00% covered (danger)
0.00%
0 / 1
354.78
 checkForLocked
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 checkNoErrorAchievements
81.54% covered (warning)
81.54%
53 / 65
0.00% covered (danger)
0.00%
0 / 1
18.82
 checkTimeModeAchievements
35.40% covered (danger)
35.40%
40 / 113
0.00% covered (danger)
0.00%
0 / 1
2273.83
 checkRatingAchievements
54.44% covered (warning)
54.44%
49 / 90
0.00% covered (danger)
0.00%
0 / 1
95.92
 checkLevelAchievements
72.22% covered (warning)
72.22%
65 / 90
0.00% covered (danger)
0.00%
0 / 1
42.63
 checkSetCompletedAchievements
19.72% covered (danger)
19.72%
14 / 71
0.00% covered (danger)
0.00%
0 / 1
166.54
 setAchievementSpecial
0.00% covered (danger)
0.00%
0 / 184
0.00% covered (danger)
0.00%
0 / 1
4160
 checkSetAchievements
18.10% covered (danger)
18.10%
40 / 221
0.00% covered (danger)
0.00%
0 / 1
2533.09
 getXPJump
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
72
 getHealth
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
462
 updateXP
75.00% covered (warning)
75.00%
36 / 48
0.00% covered (danger)
0.00%
0 / 1
18.52
 getPartitionRange
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
56
 handleSearchSettings
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 signIn
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 beforeFilter
83.58% covered (warning)
83.58%
280 / 335
0.00% covered (danger)
0.00%
0 / 1
93.31
 afterFilter
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3App::uses('Auth', 'Utility');
4App::uses('TsumegoFilters', 'Utility');
5
6class AppController extends Controller {
7    public $viewClass = 'App';
8
9    public $components = [
10        'Session',
11        //'DebugKit.Toolbar',
12        'Flash',
13        'PlayResultProcessor',
14        'TimeMode',
15    ];
16
17    public static function processSGF($sgf) {
18        $aw = strpos($sgf, 'AW');
19        $ab = strpos($sgf, 'AB');
20        $boardSizePos = strpos($sgf, 'SZ');
21        $boardSize = 19;
22        $sgfArr = str_split($sgf);
23        if ($boardSizePos !== false) {
24            $boardSize = $sgfArr[$boardSizePos + 3] . '' . $sgfArr[$boardSizePos + 4];
25        }
26        if (substr($boardSize, 1) == ']') {
27            $boardSize = substr($boardSize, 0, 1);
28        }
29
30        $black = AppController::getInitialPosition($ab, $sgfArr, 'x');
31        $white = AppController::getInitialPosition($aw, $sgfArr, 'o');
32        $stones = array_merge($black, $white);
33
34        $board = [];
35        for ($i = 0; $i < 19; $i++) {
36            $board[$i] = [];
37            for ($j = 0; $j < 19; $j++) {
38                $board[$i][$j] = '-';
39            }
40        }
41        $lowestX = 18;
42        $lowestY = 18;
43        $highestX = 0;
44        $highestY = 0;
45        $stonesCount = count($stones);
46        for ($i = 0; $i < $stonesCount; $i++) {
47            if ($stones[$i][0] < $lowestX) {
48                $lowestX = $stones[$i][0];
49            }
50            if ($stones[$i][0] > $highestX) {
51                $highestX = $stones[$i][0];
52            }
53            if ($stones[$i][1] < $lowestY) {
54                $lowestY = $stones[$i][1];
55            }
56            if ($stones[$i][1] > $highestY) {
57                $highestY = $stones[$i][1];
58            }
59        }
60        if (18 - $lowestX < $lowestX) {
61            $stones = AppController::xFlip($stones);
62        }
63        if (18 - $lowestY < $lowestY) {
64            $stones = AppController::yFlip($stones);
65        }
66        $highestX = 0;
67        $highestY = 0;
68        $stonesCount = count($stones);
69        for ($i = 0; $i < $stonesCount; $i++) {
70            if ($stones[$i][0] > $highestX) {
71                $highestX = $stones[$i][0];
72            }
73            if ($stones[$i][1] > $highestY) {
74                $highestY = $stones[$i][1];
75            }
76            $board[$stones[$i][0]][$stones[$i][1]] = $stones[$i][2];
77        }
78        $tInfo = [];
79        $tInfo[0] = $highestX;
80        $tInfo[1] = $highestY;
81        $arr = [];
82        $arr[0] = $board;
83        $arr[1] = $stones;
84        $arr[2] = $tInfo;
85        $arr[3] = $boardSize;
86
87        return $arr;
88    }
89
90    public static function xFlip($stones) {
91        $stonesCount = count($stones);
92        for ($i = 0; $i < $stonesCount; $i++) {
93            $stones[$i][0] = 18 - $stones[$i][0];
94        }
95
96        return $stones;
97    }
98
99    public static function yFlip($stones) {
100        $stonesCount = count($stones);
101        for ($i = 0; $i < $stonesCount; $i++) {
102            $stones[$i][1] = 18 - $stones[$i][1];
103        }
104
105        return $stones;
106    }
107
108    public static function getInitialPositionEnd($pos, $sgfArr) {
109        $endCondition = 0;
110        $currentPos1 = $pos + 2;
111        $currentPos2 = $pos + 5;
112        while ($sgfArr[$currentPos1] == '[' && $sgfArr[$currentPos2] == ']') {
113            $endCondition = $currentPos2;
114            $currentPos1 += 4;
115            $currentPos2 += 4;
116        }
117
118        return $endCondition;
119    }
120
121    public static function getInitialPosition($pos, $sgfArr, $color) {
122        $arr = [];
123        $end = AppController::getInitialPositionEnd($pos, $sgfArr);
124        for ($i = $pos + 2; $i < $end; $i++) {
125            if ($sgfArr[$i] != '[' && $sgfArr[$i] != ']') {
126                array_push($arr, strtolower($sgfArr[$i]));
127            }
128        }
129        $alphabet = array_flip(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
130        $xy = true;
131        $arr2 = [];
132        $c = 0;
133        $arrCount = count($arr);
134        for ($i = 0; $i < $arrCount; $i++) {
135            $arr[$i] = $alphabet[$arr[$i]];
136            if ($xy) {
137                $arr2[$c] = [];
138                $arr2[$c][0] = $arr[$i];
139            } else {
140                $arr2[$c][1] = $arr[$i];
141                $arr2[$c][2] = $color;
142                $c++;
143            }
144            $xy = !$xy;
145        }
146
147        return $arr2;
148    }
149
150    protected function getInvisibleSets() {
151        $this->loadModel('Set');
152        $invisibleSets = [];
153        $in = $this->Set->find('all', ['conditions' => ['public' => 0]]);
154        if (!$in) {
155            $in = [];
156        }
157        foreach ($in as $item) {
158            $invisibleSets[] = $item['Set']['id'];
159        }
160
161        return $invisibleSets;
162    }
163
164    protected function getDeletedSets() {
165        $dSets = [];
166        $de = $this->Set->find('all', ['conditions' => ['public' => -1]]);
167        if (!$de) {
168            $de = [];
169        }
170        foreach ($de as $item) {
171            $dSets[] = $item['Set']['id'];
172        }
173
174        return $dSets;
175    }
176
177    /**
178     * @return void
179     */
180    public static function startPageUpdate() {
181        $str = '';
182        $latest = ClassRegistry::init('AchievementStatus')->find('all', ['limit' => 7, 'order' => 'created DESC']);
183        if (!$latest) {
184            $latest = [];
185        }
186        $latestCount = count($latest);
187        for ($i = 0; $i < $latestCount; $i++) {
188            $a = ClassRegistry::init('Achievement')->findById($latest[$i]['AchievementStatus']['achievement_id']);
189            $u = ClassRegistry::init('User')->findById($latest[$i]['AchievementStatus']['user_id']);
190            if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
191                $startPageUser = AppController::checkPicture($u);
192            } else {
193                $startPageUser = $u['User']['name'];
194            }
195            $latest[$i]['AchievementStatus']['name'] = $a['Achievement']['name'];
196            $latest[$i]['AchievementStatus']['color'] = $a['Achievement']['color'];
197            $latest[$i]['AchievementStatus']['image'] = $a['Achievement']['image'];
198            $latest[$i]['AchievementStatus']['user'] = $startPageUser;
199            $str .= '<div class="quote1"><div class="quote1a"><a href="/achievements/view/' . $a['Achievement']['id'] . '"><img src="/img/' . $a['Achievement']['image'] . '.png" width="34px"></a></div>';
200            $str .= '<div class="quote1b">Achievement gained by ' . $startPageUser . ':<br><div class=""><b>' . $a['Achievement']['name'] . '</b></div></div></div>';
201        }
202        file_put_contents('mainPageAjax.txt', $str);
203    }
204
205    /**
206     * @return void
207     */
208    protected function uotd() { //routine1
209        $this->loadModel('User');
210        $this->loadModel('DayRecord');
211        $this->loadModel('TsumegoAttempt');
212        $this->loadModel('Achievement');
213        $this->loadModel('AchievementCondition');
214        $today = date('Y-m-d');
215        $ux2 = $this->User->find('all', [
216            'limit' => '8',
217            'order' => 'reuse3 DESC',
218            'conditions' => [
219                'NOT' => ['id' => [33]],
220            ],
221        ]);
222        if (!$ux2) {
223            $ux2 = [];
224        }
225        $last = $this->DayRecord->find('all', ['limit' => '7', 'order' => 'date DESC']);
226        if (!$last) {
227            $last = [];
228        }
229        $lastUotds = [];
230        $lastUsers = [];
231        foreach ($last as $item) {
232            $lastUotds[] = $item['DayRecord']['user_id'];
233        }
234        foreach ($ux2 as $item) {
235            $lastUsers[] = $item['User']['id'];
236        }
237        $resultUser = 72;
238        $lastUsersCount = count($lastUsers);
239        for ($i = 0; $i < $lastUsersCount; $i++) {
240            $foundUser = false;
241            $lastUotdsCount = count($lastUotds);
242            for ($j = 0; $j < $lastUotdsCount; $j++) {
243                if ($lastUsers[$i] == $lastUotds[$j]) {
244                    $foundUser = true;
245                }
246            }
247            if (!$foundUser) {
248                $resultUser = $lastUsers[$i];
249
250                break;
251            }
252        }
253        $ux = $this->User->findById($resultUser);
254
255        $recentlyUsed = [];
256        $d = 1;
257        while ($d <= 10) {
258            $ru = $this->DayRecord->find('first', ['conditions' => ['date' => date('Y-m-d', strtotime('-' . $d . ' days'))]]);
259            if ($ru) {
260                array_push($recentlyUsed, $ru);
261            }
262            $d++;
263        }
264        $currentQuote = 'q01';
265        $newQuote = 'q01';
266        $quoteChosen = false;
267        while (!$quoteChosen) {
268            $newQuote = rand(1, 45);
269            if ($newQuote < 10) {
270                $newQuote = 'q0' . $newQuote;
271            } else {
272                $newQuote = 'q' . $newQuote;
273            }
274
275            $f = false;
276            $recentlyUsedCount = count($recentlyUsed);
277            for ($i = 0; $i < $recentlyUsedCount; $i++) {
278                if ($newQuote == $recentlyUsed[$i]['DayRecord']['quote']) {
279                    $f = true;
280                }
281            }
282            if (!$f) {
283                $quoteChosen = true;
284            }
285        }
286        $currentQuote = $newQuote;
287        $dayUserRand = 1;
288        $uotdChosen = false;
289        while (!$uotdChosen) {
290            $dayUserRand = rand(1, 39);
291            $f = false;
292            $recentlyUsedCount = count($recentlyUsed);
293            for ($i = 0; $i < $recentlyUsedCount; $i++) {
294                if ($dayUserRand == $recentlyUsed[$i]['DayRecord']['userbg']) {
295                    $f = true;
296                }
297            }
298            if (!$f) {
299                $uotdChosen = true;
300            }
301        }
302        $activity = $this->TsumegoAttempt->find('all', ['limit' => 40000, 'conditions' => ['DATE(TsumegoAttempt.created)' => date('Y-m-d', strtotime('yesterday'))]]);
303        if (!$activity) {
304            $activity = [];
305        }
306        $visitedProblems = count($activity);
307
308        //how many users today
309        $usersNum = [];
310        $activity = $this->User->find('all', ['limit' => 400, 'order' => 'created DESC']);
311        if (!$activity) {
312            $activity = [];
313        }
314        $activityCount = count($activity);
315        for ($i = 0; $i < $activityCount; $i++) {
316            $a = new DateTime($activity[$i]['User']['created']);
317            if ($a->format('Y-m-d') == $today) {
318                array_push($usersNum, $activity[$i]['User']);
319            }
320        }
321        $gemRand1 = rand(0, 2);
322        $gemRand2 = rand(0, 2);
323        $gemRand3 = rand(0, 2);
324
325        $arch1 = ClassRegistry::init('Achievement')->findById(111);
326        if ($gemRand1 == 0) {
327            $arch1['Achievement']['description'] = 'Has a chance to trigger once a day on an easy ddk problem.';
328        } elseif ($gemRand1 == 1) {
329            $arch1['Achievement']['description'] = 'Has a chance to trigger once a day on a regular ddk problem.';
330        } elseif ($gemRand1 == 2) {
331            $arch1['Achievement']['description'] = 'Has a chance to trigger once a day on a difficult ddk problem.';
332        }
333        ClassRegistry::init('Achievement')->save($arch1);
334        $arch2 = ClassRegistry::init('Achievement')->findById(112);
335        if ($gemRand2 == 0) {
336            $arch2['Achievement']['description'] = 'Has a chance to trigger once a day on an easy sdk problem.';
337        } elseif ($gemRand2 == 1) {
338            $arch2['Achievement']['description'] = 'Has a chance to trigger once a day on a regular sdk problem.';
339        } elseif ($gemRand2 == 2) {
340            $arch2['Achievement']['description'] = 'Has a chance to trigger once a day on a difficult sdk problem.';
341        }
342        ClassRegistry::init('Achievement')->save($arch2);
343        $arch3 = ClassRegistry::init('Achievement')->findById(113);
344        if ($gemRand3 == 0) {
345            $arch3['Achievement']['description'] = 'Has a chance to trigger once a day on an easy dan problem.';
346        } elseif ($gemRand3 == 1) {
347            $arch3['Achievement']['description'] = 'Has a chance to trigger once a day on a regular dan problem.';
348        } elseif ($gemRand3 == 2) {
349            $arch3['Achievement']['description'] = 'Has a chance to trigger once a day on a difficult dan problem.';
350        }
351        ClassRegistry::init('Achievement')->save($arch3);
352
353        $this->DayRecord->create();
354        $dateUser = [];
355        $dateUser['DayRecord']['user_id'] = $ux['User']['id'];
356        $dateUser['DayRecord']['date'] = $today;
357        $dateUser['DayRecord']['solved'] = $ux['User']['reuse3'];
358        $dateUser['DayRecord']['quote'] = $currentQuote;
359        $dateUser['DayRecord']['userbg'] = $dayUserRand;
360        $dateUser['DayRecord']['tsumego'] = $this->getTsumegoOfTheDay();
361        $dateUser['DayRecord']['newTsumego'] = $this->getNewTsumego();
362        $dateUser['DayRecord']['usercount'] = count($usersNum);
363        $dateUser['DayRecord']['visitedproblems'] = $visitedProblems;
364        $dateUser['DayRecord']['gems'] = $gemRand1 . '-' . $gemRand2 . '-' . $gemRand3;
365        $dateUser['DayRecord']['gemCounter1'] = 0;
366        $dateUser['DayRecord']['gemCounter2'] = 0;
367        $dateUser['DayRecord']['gemCounter3'] = 0;
368        $this->DayRecord->save($dateUser);
369
370        ClassRegistry::init('AchievementCondition')->create();
371        $achievementCondition = [];
372        $achievementCondition['AchievementCondition']['user_id'] = $ux['User']['id'];
373        $achievementCondition['AchievementCondition']['set_id'] = 0;
374        $achievementCondition['AchievementCondition']['category'] = 'uotd';
375        $achievementCondition['AchievementCondition']['value'] = 1;
376        ClassRegistry::init('AchievementCondition')->save($achievementCondition);
377
378        //delete duplicated DayRecords
379        $dr = $this->DayRecord->find('all');
380        if (!$dr) {
381            $dr = [];
382        }
383        $duplicates = [];
384        $drCount = count($dr);
385        for ($i = 0; $i < $drCount; $i++) {
386            $alreadyFound = [];
387            for ($j = 0; $j < $drCount; $j++) {
388                if ($i != $j && $dr[$j]['DayRecord']['date'] == $dr[$i]['DayRecord']['date']) {
389                    $found = false;
390                    $alreadyFoundCount = count($alreadyFound);
391                    for ($k = 0; $k < $alreadyFoundCount; $k++) {
392                        if ($alreadyFound[$k]['DayRecord']['id'] == $dr[$i]['DayRecord']['id'] || $alreadyFound[$k]['DayRecord']['id'] == $dr[$j]['DayRecord']['id']) {
393                            $found = true;
394                        }
395                    }
396                    if (!$found) {
397                        array_push($duplicates, $dr[$i]['DayRecord']['date']);
398                        array_push($alreadyFound, $dr[$i]);
399                    }
400                }
401            }
402        }
403        $duplicates = array_count_values($duplicates);
404        foreach ($duplicates as $key => $value) {
405            while ($duplicates[$key] > 1) {
406                $drd = $this->DayRecord->find('first', ['conditions' => ['date' => $key]]);
407                if ($drd) {
408                    $this->DayRecord->delete($drd['DayRecord']['id']);
409                }
410                $duplicates[$key]--;
411            }
412        }
413    }
414
415    public static function deleteUnusedStatuses(int $uid): void {
416        $s = ClassRegistry::init('Set')->find('all', [
417            'conditions' => [
418                'OR' => [
419                    ['public' => 1],
420                    ['public' => 0],
421                ],
422            ],
423        ]) ?: [];
424        $ids = [];
425        $sCount = count($s);
426        for ($i = 0; $i < $sCount; $i++) {
427            $tSet = TsumegoUtil::collectTsumegosFromSet($s[$i]['Set']['id']);
428            foreach ($tSet as $item) {
429                $ids[] = $item['Tsumego']['id'];
430            }
431        }
432        $ut = ClassRegistry::init('TsumegoStatus')->find('all', [
433            'conditions' => [
434                'user_id' => $uid,
435                'NOT' => [
436                    'tsumego_id' => $ids,
437                ],
438            ],
439        ]);
440        if (!$ut) {
441            $ut = [];
442        }
443        $utCount = count($ut);
444        for ($i = 0; $i < $utCount; $i++) {
445            $test1 = ClassRegistry::init('Tsumego')->findById($ut[$i]['TsumegoStatus']['tsumego_id']);
446            $test2 = ClassRegistry::init('SetConnection')->find('first', ['conditions' => ['tsumego_id' => $test1['Tsumego']['id']]]);
447            if (!$test2) {
448                ClassRegistry::init('TsumegoStatus')->delete($ut[$i]['TsumegoStatus']['id']);
449
450                continue;
451            }
452            $test3 = ClassRegistry::init('Set')->find('first', [
453                'id' => $test2['SetConnection']['set_id'],
454                'OR' => [
455                    ['public' => 1],
456                    ['public' => 0],
457                ],
458            ]);
459            if ($test3 == null) {
460                ClassRegistry::init('TsumegoStatus')->delete($ut[$i]['TsumegoStatus']['id']);
461            }
462        }
463    }
464
465    protected function saveSolvedNumber($uid) {
466        $this->loadModel('User');
467        $this->loadModel('TsumegoStatus');
468        $this->loadModel('Set');
469        $this->loadModel('SetConnection');
470
471        $solvedUts2 = 0;
472        $tsumegos = $this->SetConnection->find('all');
473        if (!$tsumegos) {
474            $tsumegos = [];
475        }
476        $uts = $this->TsumegoStatus->find('all', ['order' => 'created DESC', 'conditions' => ['user_id' => $uid]]);
477        if (!$uts) {
478            $uts = [];
479        }
480        $setKeys = [];
481        $setArray = $this->Set->find('all', ['conditions' => ['public' => 1]]);
482        if (!$setArray) {
483            $setArray = [];
484        }
485
486        $setArrayCount = count($setArray);
487        for ($i = 0; $i < $setArrayCount; $i++) {
488            $setKeys[$setArray[$i]['Set']['id']] = $setArray[$i]['Set']['id'];
489        }
490
491        $scs = [];
492        $tsumegosCount = count($tsumegos);
493        for ($j = 0; $j < $tsumegosCount; $j++) {
494            if (!isset($scs[$tsumegos[$j]['SetConnection']['tsumego_id']])) {
495                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']] = 1;
496            } else {
497                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']]++;
498            }
499        }
500        $utsCount = count($uts);
501        for ($j = 0; $j < $utsCount; $j++) {
502            if ($uts[$j]['TsumegoStatus']['status'] == 'S' || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C') {
503                if (isset($scs[$uts[$j]['TsumegoStatus']['tsumego_id']])) {
504                    $solvedUts2 += $scs[$uts[$j]['TsumegoStatus']['tsumego_id']];
505                }
506            }
507        }
508        Auth::getUser()['solved'] = $solvedUts2;
509        Auth::saveUser();
510
511        return $solvedUts2;
512    }
513
514    public static function convertEloToXp($elo) {
515        $xp = round(pow($elo / 100, 1.55) - 6);
516        if ($xp < 10) {
517            $xp = 10;
518        }
519
520        return $xp;
521    }
522
523    /**
524     * @return void
525     */
526    protected function resetUserElos() {
527        $this->loadModel('User');
528
529        $u = $this->User->find('all', [
530            'conditions' => [
531                'id >=' => 15000,
532                'id <=' => 19000,
533            ],
534        ]);
535        if (!$u) {
536            $u = [];
537        }
538
539        $uCount = count($u);
540        for ($i = 0; $i < $uCount; $i++) {
541            $u[$i]['User']['rating'] = 900;
542            $u[$i]['User']['solved2'] = 0;
543            $this->User->save($u[$i]);
544        }
545    }
546
547    public static function getNewElo($diff, $eloBigger, $activityValue, $tid, $outcome) {
548        $return = [];
549        $t = ClassRegistry::init('Tsumego')->findById($tid);
550        if ($t['Tsumego']['activity_value'] > 300) {
551            $t['Tsumego']['activity_value'] = 300;
552        }
553        $tsumegoAvtivityValue = max(round((2 / 3) * $t['Tsumego']['activity_value']), 15);
554        $kFactor1 = 1;
555        $kFactor2 = 1;
556        if (Auth::isLoggedIn()) {
557            $rating = Auth::getUser()['rating'];
558            if ($rating >= 1500) {
559                $kFactor1 = 1.5;
560                $kFactor2 = 0.9;
561            } elseif ($rating >= 1800) {
562                $kFactor1 = 2;
563                $kFactor2 = 0.8;
564            } elseif ($rating >= 2100) {
565                $kFactor1 = 2.5;
566                $kFactor2 = 0.7;
567            } elseif ($rating >= 2400) {
568                $kFactor1 = 3;
569                $kFactor2 = 0.6;
570            }
571        }
572        if ($diff == 0) {
573            $diff = .1;
574        }
575        $tWin = 0;
576        $tLoss = 0;
577        $uWin = 0;
578        $uLoss = 0;
579        if ($eloBigger == 'u') {
580            $tWin = $diff / $tsumegoAvtivityValue;
581            $tLoss = 0;
582
583            $uWin = log($diff, 2) - $diff / 70;
584            if ($diff <= 100) {
585                $uWin = 5;
586            }
587            $uWin *= $kFactor2;
588            if ($uWin < 1) {
589                $uWin = 1 - $diff / 1400;
590            }
591            if ($uWin > 5) {
592                $uWin = 5;
593            }
594
595            $uLoss = log($diff, 2) * 2.2 - 12;
596            $uLoss *= $kFactor2;
597            if ($uLoss < 5) {
598                $uLoss = 5;
599            }
600            $uLoss *= -1;
601
602            if ($diff > 1000) {
603                $uWin = 0;
604                $uLoss = 0;
605            }
606        } elseif ($eloBigger == 't') {
607            $tWin = 0;
608            $tLoss = $diff / $tsumegoAvtivityValue * (-1);
609
610            $uWin = log($diff, 2) * 2.2 - 11;
611            $uWin *= $kFactor2;
612            if ($uWin < 5) {
613                $uWin = 5;
614            }
615
616            $uLoss = log($diff, 2) - $diff / 70 - 1;
617            if ($diff < 100) {
618                $uLoss = 4;
619            }
620            $uLoss *= $kFactor2;
621            if ($uLoss < 1) {
622                $uLoss = 1 - $diff / 1400;
623            }
624            $uLoss *= -1;
625        }
626
627        $activityValueBase = 1;
628        if ($activityValue < 30) {
629            $activityValueAdd = 0;
630        } elseif ($activityValue < 50) {
631            $activityValueAdd = 0.6;
632        } elseif ($activityValue < 70) {
633            $activityValueAdd = 1.2;
634        } else {
635            $activityValueAdd = 1.8;
636        }
637
638        $activityValueSum = $activityValueBase + ($activityValueAdd / $kFactor1);
639
640        if ($outcome == 'w') {
641            $return['user'] = round($uWin * $activityValueSum, 2);
642            $return['user2'] = $uWin * $activityValueSum;
643            $return['tsumego'] = round($tLoss);
644            $return['tsumego2'] = $tLoss;
645        } else {
646            $return['user'] = round($uLoss * $activityValueSum, 2);
647            $return['user2'] = $uLoss * $activityValueSum;
648            $return['tsumego'] = round($tWin);
649            $return['tsumego2'] = $tWin;
650        }
651
652        return $return;
653    }
654
655    /**
656     * @param int $uid User ID
657     * @param string $action Action type
658     *
659     * @return void
660     */
661    public static function handleContribution($uid, $action) {
662        $uc = ClassRegistry::init('UserContribution')->find('first', ['conditions' => ['user_id' => $uid]]);
663        if ($uc == null) {
664            $uc = [];
665            $uc['UserContribution']['user_id'] = $uid;
666            $uc['UserContribution']['added_tag'] = 0;
667            $uc['UserContribution']['created_tag'] = 0;
668            $uc['UserContribution']['made_proposal'] = 0;
669            $uc['UserContribution']['reviewed'] = 0;
670            $uc['UserContribution']['score'] = 0;
671            ClassRegistry::init('UserContribution')->create();
672        }
673        $uc['UserContribution'][$action] += 1;
674        $uc['UserContribution']['score']
675        = $uc['UserContribution']['added_tag']
676        + $uc['UserContribution']['created_tag'] * 3
677        + $uc['UserContribution']['made_proposal'] * 5
678        + $uc['UserContribution']['reviewed'] * 2;
679        ClassRegistry::init('UserContribution')->save($uc);
680    }
681
682    public static function getAllTags($not) {
683        $a = [];
684        $notApproved = ClassRegistry::init('TagName')->find('all', ['conditions' => ['approved' => 0]]);
685        if (!$notApproved) {
686            $notApproved = [];
687        }
688        $notCount = count($not);
689        for ($i = 0; $i < $notCount; $i++) {
690            array_push($a, $not[$i]['Tag']['tag_name_id']);
691        }
692        $notApprovedCount = count($notApproved);
693        for ($i = 0; $i < $notApprovedCount; $i++) {
694            array_push($a, $notApproved[$i]['TagName']['id']);
695        }
696        $tn = ClassRegistry::init('TagName')->find('all', [
697            'conditions' => [
698                'NOT' => ['id' => $a],
699            ],
700        ]);
701        if (!$tn) {
702            $tn = [];
703        }
704        $sorted = [];
705        $keys = [];
706        $tnCount = count($tn);
707        for ($i = 0; $i < $tnCount; $i++) {
708            array_push($sorted, $tn[$i]['TagName']['name']);
709            $keys[$tn[$i]['TagName']['name']] = $tn[$i];
710        }
711        sort($sorted);
712        $s2 = [];
713        $sortedCount = count($sorted);
714        for ($i = 0; $i < $sortedCount; $i++) {
715            array_push($s2, $keys[$sorted[$i]]);
716        }
717
718        return $s2;
719    }
720
721    /**
722     * @param int|null $range Range parameter
723     *
724     * @return void
725     */
726    protected function userRefresh($range = null) {
727        $this->loadModel('User');
728        $this->loadModel('TsumegoStatus');
729        $u = [];
730        if ($range == 1) {
731            $u = $this->User->find('all', [
732                'order' => 'id DESC',
733                'conditions' => [
734                    'id <' => 2000,
735                ],
736            ]);
737            if (!$u) {
738                $u = [];
739            }
740        } elseif ($range == 2) {
741            $u = $this->User->find('all', [
742                'order' => 'id DESC',
743                'conditions' => [
744                    'id >=' => 2000,
745                    'id <' => 4000,
746                ],
747            ]);
748            if (!$u) {
749                $u = [];
750            }
751        } elseif ($range == 3) {
752            $u = $this->User->find('all', [
753                'order' => 'id DESC',
754                'conditions' => [
755                    'id >=' => 4000,
756                    'id <' => 6000,
757                ],
758            ]);
759            if (!$u) {
760                $u = [];
761            }
762        } elseif ($range == 4) {
763            $u = $this->User->find('all', [
764                'order' => 'id DESC',
765                'conditions' => [
766                    'id >=' => 6000,
767                    'id <' => 8000,
768                ],
769            ]);
770            if (!$u) {
771                $u = [];
772            }
773        } elseif ($range == 5) {
774            $u = $this->User->find('all', [
775                'order' => 'id DESC',
776                'conditions' => [
777                    'id >=' => 8000,
778                    'id <' => 10000,
779                ],
780            ]);
781            if (!$u) {
782                $u = [];
783            }
784        } elseif ($range == 6) {
785            $u = $this->User->find('all', [
786                'order' => 'id DESC',
787                'conditions' => [
788                    'id >=' => 10000,
789                    'id <' => 12000,
790                ],
791            ]);
792            if (!$u) {
793                $u = [];
794            }
795        } elseif ($range == 7) {
796            $u = $this->User->find('all', [
797                'order' => 'id DESC',
798                'conditions' => [
799                    'id >=' => 12000,
800                    'id <' => 14000,
801                ],
802            ]);
803            if (!$u) {
804                $u = [];
805            }
806        } elseif ($range == 8) {
807            $u = $this->User->find('all', [
808                'order' => 'id DESC',
809                'conditions' => [
810                    'id >=' => 14000,
811                    'id <' => 16000,
812                ],
813            ]);
814            if (!$u) {
815                $u = [];
816            }
817        } elseif ($range == 9) {
818            $u = $this->User->find('all', [
819                'order' => 'id DESC',
820                'conditions' => [
821                    'id >=' => 16000,
822                    'id <' => 18000,
823                ],
824            ]);
825            if (!$u) {
826                $u = [];
827            }
828        } elseif ($range == 10) {
829            $u = $this->User->find('all', [
830                'order' => 'id DESC',
831                'conditions' => [
832                    'id >=' => 18000,
833                    'id <' => 20000,
834                ],
835            ]);
836            if (!$u) {
837                $u = [];
838            }
839        } elseif ($range == 11) {
840            $u = $this->User->find('all', [
841                'order' => 'id DESC',
842                'conditions' => [
843                    'id >=' => 20000,
844                    'id <' => 22000,
845                ],
846            ]);
847            if (!$u) {
848                $u = [];
849            }
850        } elseif ($range == 12) {
851            $u = $this->User->find('all', [
852                'order' => 'id DESC',
853                'conditions' => [
854                    'id >=' => 22000,
855                ],
856            ]);
857            if (!$u) {
858                $u = [];
859            }
860        }
861        $uCount = count($u);
862        for ($i = 0; $i < $uCount; $i++) {
863            $this->User->create();
864            $u[$i]['User']['reuse2'] = 0;//#
865            $u[$i]['User']['reuse3'] = 0;//xp
866            $u[$i]['User']['reuse4'] = 0;//daily maximum
867            $u[$i]['User']['damage'] = 0;
868            $u[$i]['User']['readingTrial'] = 30;
869            $u[$i]['User']['promoted'] += 1;
870            $u[$i]['User']['lastRefresh'] = date('Y-m-d');
871            if ($u[$i]['User']['isAdmin'] > 0) {
872                $u[$i]['User']['revelation'] = 20;
873            }
874            $this->User->save($u[$i]);
875        }
876        $uts = $this->TsumegoStatus->find('all', ['conditions' => ['status' => 'F']]);
877        if (!$uts) {
878            $uts = [];
879        }
880        $utsCount = count($uts);
881        for ($i = 0; $i < $utsCount; $i++) {
882            $uts[$i]['TsumegoStatus']['status'] = 'V';
883            //$this->TsumegoStatus->save($uts[$i]);
884        }
885        $uts = $this->TsumegoStatus->find('all', ['conditions' => ['status' => 'X']]);
886        if (!$uts) {
887            $uts = [];
888        }
889        $utsCount = count($uts);
890        for ($i = 0; $i < $utsCount; $i++) {
891            $uts[$i]['TsumegoStatus']['status'] = 'W';
892            //$this->TsumegoStatus->save($uts[$i]);
893        }
894    }
895
896    /**
897     * @return void
898     */
899    protected function deleteUserBoards() {
900        $this->loadModel('UserBoard');
901        $this->UserBoard->deleteAll(['1 = 1']);
902    }
903
904    /**
905     * @return void
906     */
907    protected function halfXP() {
908        $this->loadModel('TsumegoStatus');
909        $this->loadModel('DayRecord');
910        $week = $this->TsumegoStatus->find('all', ['order' => 'created DESC', 'conditions' => ['status' => 'S']]);
911        if (!$week) {
912            $week = [];
913        }
914        $oneWeek = date('Y-m-d H:i:s', strtotime('-7 days'));
915        $weekCount = count($week);
916        for ($i = 0; $i < $weekCount; $i++) {
917            if ($week[$i]['TsumegoStatus']['created'] < $oneWeek) {
918                if ($week[$i]['TsumegoStatus']['status'] == 'S') {
919                    $week[$i]['TsumegoStatus']['status'] = 'W';
920                    //$this->TsumegoStatus->save($week[$i]);
921                }
922            }
923        }
924    }
925
926    protected function getNewTsumego() {
927        $this->loadModel('Schedule');
928        $date = date('Y-m-d', strtotime('today'));
929        $s = $this->Schedule->find('all', ['conditions' => ['date' => $date]]);
930        if (!$s) {
931            $s = [];
932        }
933        $id = 0;
934        $sCount = count($s);
935        for ($i = 0; $i < $sCount; $i++) {
936            $id = $this->publishSingle($s[$i]['Schedule']['tsumego_id'], $s[$i]['Schedule']['set_id'], $s[$i]['Schedule']['date']);
937            $s[$i]['Schedule']['tsumego_id'] = $id;
938            $s[$i]['Schedule']['published'] = 1;
939            $this->Schedule->save($s[$i]);
940        }
941
942        return $id;
943    }
944
945    protected function publishSingle($t = null, $to = null, $date = null) {
946        $this->loadModel('Tsumego');
947        $this->loadModel('Sgf');
948        $this->loadModel('SetConnection');
949        $this->loadModel('PublishDate');
950        $ts = $this->Tsumego->findById($t);
951
952        $id = $this->Tsumego->find('first', ['limit' => 1, 'order' => 'id DESC']);
953        if (!$id) {
954            return null;
955        }
956        $id = $id['Tsumego']['id'];
957        $id += 1;
958
959        $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $ts['Tsumego']['id']]]);
960        if ($scT != null) {
961            $scT['SetConnection']['set_id'] = $to;
962            $scT['SetConnection']['tsumego_id'] = $id;
963            $scT['SetConnection']['num'] = $ts['Tsumego']['num'];
964            $this->SetConnection->save($scT);
965        } else {
966            $scT = [];
967            $scT['SetConnection']['set_id'] = $to;
968            $scT['SetConnection']['tsumego_id'] = $id;
969            $scT['SetConnection']['num'] = $ts['Tsumego']['num'];
970            $this->SetConnection->create();
971            $this->SetConnection->save($scT);
972        }
973
974        $sid = $ts['Tsumego']['id'];
975        $ts['Tsumego']['id'] = $id;
976        $ts['Tsumego']['created'] = $date . ' 22:00:00';
977        $ts['Tsumego']['solved'] = 0;
978        $ts['Tsumego']['failed'] = 0;
979        $ts['Tsumego']['userWin'] = 0;
980        $ts['Tsumego']['userLoss'] = 0;
981        $this->Tsumego->create();
982        $this->Tsumego->save($ts);
983        $this->Tsumego->delete($sid);
984
985        $sgfs = $this->Sgf->find('all', ['conditions' => ['tsumego_id' => $t]]);
986        if (!$sgfs) {
987            $sgfs = [];
988        }
989        $sgfsCount = count($sgfs);
990        for ($i = 0; $i < $sgfsCount; $i++) {
991            $sgfs[$i]['Sgf']['tsumego_id'] = $id;
992            $this->Sgf->save($sgfs[$i]);
993        }
994        $x = [];
995        $x['PublishDate']['date'] = $date . ' 22:00:00';
996        $x['PublishDate']['tsumego_id'] = $id;
997        $this->PublishDate->create();
998        $this->PublishDate->save($x);
999
1000        return $id;
1001    }
1002
1003    protected function getTsumegoOfTheDay() {
1004        $this->loadModel('TsumegoAttempt');
1005        $this->loadModel('TsumegoRatingAttempt');
1006        $this->loadModel('Schedule');
1007        $this->loadModel('Tsumego');
1008        $this->loadModel('SetConnection');
1009
1010        $ut = $this->TsumegoRatingAttempt->find('all', ['limit' => 10000, 'order' => 'created DESC', 'conditions' => ['status' => 'S']]);
1011        if (!$ut) {
1012            $ut = [];
1013        }
1014        $out = $this->TsumegoAttempt->find('all', ['limit' => 30000, 'order' => 'created DESC', 'conditions' => ['gain >=' => 40]]);
1015        if (!$out) {
1016            $out = [];
1017        }
1018
1019        $date = date('Y-m-d', strtotime('yesterday'));
1020        $s = $this->Schedule->find('all', ['conditions' => ['date' => $date]]);
1021        if (!$s) {
1022            $s = [];
1023        }
1024        $ids = [];
1025        $utCount = count($ut);
1026        for ($i = 0; $i < $utCount; $i++) {
1027            $date2 = new DateTime($ut[$i]['TsumegoRatingAttempt']['created']);
1028            $date2 = $date2->format('Y-m-d');
1029            if ($date === $date2) {
1030                array_push($ids, $ut[$i]['TsumegoRatingAttempt']['tsumego_id']);
1031            }
1032        }
1033        $ids = array_count_values($ids);
1034        $highest = 0;
1035        $best = [];
1036        foreach ($ids as $key => $value) {
1037            if ($value > $highest) {
1038                $highest = $value;
1039            }
1040        }
1041        foreach ($ids as $key => $value) {
1042            if ($value == $highest) {
1043                $x = [];
1044                $x[$key] = $value;
1045                array_push($best, $x);
1046            }
1047        }
1048        $ids2 = [];
1049        $out2 = [];
1050        $outCount = count($out);
1051        for ($i = 0; $i < $outCount; $i++) {
1052            $date2 = new DateTime($out[$i]['TsumegoAttempt']['created']);
1053            $date2 = $date2->format('Y-m-d');
1054            if ($date === $date2) {
1055                array_push($ids2, $out[$i]['TsumegoAttempt']['tsumego_id']);
1056                array_push($out2, $out[$i]);
1057            }
1058        }
1059        $ids2 = array_count_values($ids2);
1060        $highest = 0;
1061        $best2 = [];
1062        foreach ($ids2 as $key => $value) {
1063            if ($value > $highest) {
1064                $highest = $value;
1065            }
1066        }
1067        $done = false;
1068        $found = 0;
1069        $decrement = 0;
1070        $best3 = [];
1071        $findNum = 20;
1072        while (!$done) {
1073            foreach ($ids2 as $key => $value) {
1074                if ($value == $highest - $decrement) {
1075                    array_push($best2, $key);
1076                    array_push($best3, $value);
1077                    $found++;
1078                }
1079            }
1080            $decrement++;
1081            if ($found < $findNum) {
1082                $done = false;
1083            } else {
1084                $done = true;
1085            }
1086        }
1087        $newBest = [];
1088        for ($j = 0; $j < $findNum; $j++) {
1089            $newBest[$j] = [];
1090        }
1091        $out2Count = count($out2);
1092        for ($i = 0; $i < $out2Count; $i++) {
1093            for ($j = 0; $j < $findNum; $j++) {
1094                if ($out2[$i]['TsumegoAttempt']['tsumego_id'] == $best2[$j]) {
1095                    $x = [];
1096                    $x['tid'] = $out2[$i]['TsumegoAttempt']['tsumego_id'];
1097                    $tx = $this->Tsumego->findById($x['tid']);
1098                    $scT = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $tx['Tsumego']['id']]]);
1099                    $tx['Tsumego']['set_id'] = $scT['SetConnection']['set_id'];
1100                    $x['sid'] = $tx['Tsumego']['set_id'];
1101                    $x['status'] = $out2[$i]['TsumegoAttempt']['solved'];
1102                    $x['seconds'] = $out2[$i]['TsumegoAttempt']['seconds'];
1103
1104                    $newBest[$j][] = $x;
1105                }
1106            }
1107        }
1108        $newBestCount = count($newBest);
1109        for ($i = 0; $i < $newBestCount; $i++) {
1110            $sum = 0;
1111            $newBestICount = count($newBest[$i]);
1112            for ($j = 0; $j < $newBestICount; $j++) {
1113                if ($newBest[$i][$j]['seconds'] != null) {
1114                    if ($newBest[$i][$j]['seconds'] > 300) {
1115                        $newBest[$i][$j]['seconds'] = 300;
1116                    }
1117                    $sum += $newBest[$i][$j]['seconds'];
1118                }
1119            }
1120            $sum = $sum * count($newBest[$i]);
1121            $newBest[$i]['sum'] = $sum;
1122        }
1123        $highest = 0;
1124        $hid = 0;
1125        $newBestCount = count($newBest);
1126        for ($i = 0; $i < $newBestCount; $i++) {
1127            if ($newBest[$i]['sum'] > $highest && $newBest[$i][0]['sid'] != 104 && $newBest[$i][0]['sid'] != 105 && $newBest[$i][0]['sid'] != 117) {
1128                $yesterday = false;
1129                $sCount = count($s);
1130                for ($j = 0; $j < $sCount; $j++) {
1131                    if ($newBest[$i][0]['tid'] == $s[$j]['Schedule']['tsumego_id']) {
1132                        $yesterday = true;
1133                    }
1134                }
1135                if (!$yesterday) {
1136                    $highest = $newBest[$i]['sum'];
1137                    $hid = $i;
1138                }
1139            }
1140        }
1141
1142        return $newBest[$hid][0]['tid'];
1143    }
1144
1145    protected function ratingMatch($elo) {
1146        if ($elo >= 3000) {
1147            $td = 10;//10d
1148        } elseif ($elo >= 2900) {
1149            $td = 10;//9d
1150        } elseif ($elo >= 2800) {
1151            $td = 10;//8d
1152        } elseif ($elo >= 2700) {
1153            $td = 10;//7d x2700
1154        } elseif ($elo >= 2600) {
1155            $td = 9;//6d x2600
1156        } elseif ($elo >= 2500) {
1157            $td = 8;//5d x2500
1158        } elseif ($elo >= 2400) {
1159            $td = 7;//4d
1160        } elseif ($elo >= 2300) {
1161            $td = 7;//3d x2350
1162        } elseif ($elo >= 2200) {
1163            $td = 6;//2d
1164        } elseif ($elo >= 2100) {
1165            $td = 6;//1d x2150
1166        } elseif ($elo >= 2000) {
1167            $td = 5;//1k
1168        } elseif ($elo >= 1900) {
1169            $td = 5;//2k x1950
1170        } elseif ($elo >= 1800) {
1171            $td = 4;//3k
1172        } elseif ($elo >= 1700) {
1173            $td = 4;//4k x1750
1174        } elseif ($elo >= 1600) {
1175            $td = 3;//5k
1176        } elseif ($elo >= 1500) {
1177            $td = 3;//6k x1500
1178        } elseif ($elo >= 1400) {
1179            $td = 3;//7k
1180        } elseif ($elo >= 1300) {
1181            $td = 2;//8k
1182        } elseif ($elo >= 1200) {
1183            $td = 2;//9k x1200
1184        } elseif ($elo >= 1100) {
1185            $td = 2;//10k
1186        } elseif ($elo >= 1000) {
1187            $td = 1;//11k
1188        } elseif ($elo >= 900) {
1189            $td = 1;//12k x900
1190        } elseif ($elo >= 800) {
1191            $td = 1;//13k
1192        } elseif ($elo >= 700) {
1193            $td = 1;//14k
1194        } elseif ($elo >= 600) {
1195            $td = 1;//15k
1196        } elseif ($elo >= 500) {
1197            $td = 1;//16k
1198        } elseif ($elo >= 400) {
1199            $td = 1;//17k
1200        } elseif ($elo >= 300) {
1201            $td = 1;//18k
1202        } elseif ($elo >= 200) {
1203            $td = 1;//19k
1204        } elseif ($elo >= 100) {
1205            $td = 1;//20k
1206        } else {
1207            $td = 1;
1208        }
1209
1210        return $td;
1211    }
1212
1213    protected function rating2($d) {
1214        if ($d == 10) {
1215            $elo = 2700;
1216        } elseif ($d == 9) {
1217            $elo = 2600;
1218        } elseif ($d == 8) {
1219            $elo = 2500;
1220        } elseif ($d == 7) {
1221            $elo = 2350;
1222        } elseif ($d == 6) {
1223            $elo = 2150;
1224        } elseif ($d == 5) {
1225            $elo = 1950;
1226        } elseif ($d == 4) {
1227            $elo = 1750;
1228        } elseif ($d == 3) {
1229            $elo = 1500;
1230        } elseif ($d == 2) {
1231            $elo = 1200;
1232        } elseif ($d == 1) {
1233            $elo = 900;
1234        } else {
1235            $elo = 1500;
1236        }
1237
1238        return $elo;
1239    }
1240
1241    public static function encrypt($str = null) {
1242        $secret_key = 'my_simple_secret_keyx';
1243        $secret_iv = 'my_simple_secret_ivx';
1244        $encrypt_method = 'AES-256-CBC';
1245        $key = hash('sha256', $secret_key);
1246        $iv = substr(hash('sha256', $secret_iv), 0, 16);
1247
1248        return base64_encode(openssl_encrypt($str, $encrypt_method, $key, 0, $iv));
1249    }
1250
1251    public static function decrypt($str = null) {
1252        $string = $str;
1253        $secret_key = 'my_simple_secret_keyx';
1254        $secret_iv = 'my_simple_secret_ivx';
1255        $encrypt_method = 'AES-256-CBC';
1256        $key = hash('sha256', $secret_key);
1257        $iv = substr(hash('sha256', $secret_iv), 0, 16);
1258
1259        return openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
1260    }
1261
1262    protected function checkPictureLarge($u) {
1263        if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
1264            return '<img class="google-profile-image-large" src="/img/google/' . $u['User']['picture'] . '">' . substr($u['User']['name'], 3);
1265        }
1266
1267        return $u['User']['name'];
1268    }
1269    public static function checkPicture($user) {
1270        if (substr($user['name'], 0, 3) == 'g__' && $user['external_id'] != null) {
1271            return '<img class="google-profile-image" src="/img/google/' . $user['picture'] . '">' . substr($user['name'], 3);
1272        }
1273
1274        return $user['name'];
1275    }
1276
1277    public static function getTsumegoRankx($t) {
1278        if ($t <= 0) {
1279            return '15k';
1280        }
1281        if ($t > 0 && $t <= 22) {
1282            $tRank = '5d';
1283        } elseif ($t <= 26.5) {
1284            $tRank = '4d';
1285        } elseif ($t <= 30) {
1286            $tRank = '3d';
1287        } elseif ($t <= 34) {
1288            $tRank = '2d';
1289        } elseif ($t <= 38) {
1290            $tRank = '1d';
1291        } elseif ($t <= 42) {
1292            $tRank = '1k';
1293        } elseif ($t <= 46) {
1294            $tRank = '2k';
1295        } elseif ($t <= 50) {
1296            $tRank = '3k';
1297        } elseif ($t <= 54.5) {
1298            $tRank = '4k';
1299        } elseif ($t <= 58.5) {
1300            $tRank = '5k';
1301        } elseif ($t <= 63) {
1302            $tRank = '6k';
1303        } elseif ($t <= 67) {
1304            $tRank = '7k';
1305        } elseif ($t <= 70.8) {
1306            $tRank = '8k';
1307        } elseif ($t <= 74.8) {
1308            $tRank = '9k';
1309        } elseif ($t <= 79) {
1310            $tRank = '10k';
1311        } elseif ($t <= 83.5) {
1312            $tRank = '11k';
1313        } elseif ($t <= 88) {
1314            $tRank = '12k';
1315        } elseif ($t <= 92) {
1316            $tRank = '13k';
1317        } elseif ($t <= 96) {
1318            $tRank = '14k';
1319        } else {
1320            $tRank = '15k';
1321        }
1322
1323        return $tRank;
1324    }
1325    protected function adjustElo($v) {
1326        $add = 0;
1327        if ($v >= 1700) {
1328            $add = 110;
1329        } elseif ($v >= 1600) {
1330            $add = 110;
1331        } elseif ($v >= 1700) {
1332            $add = 100;
1333        } elseif ($v >= 1600) {
1334            $add = 90;
1335        } elseif ($v >= 1500) {
1336            $add = 80;
1337        } elseif ($v >= 1400) {
1338            $add = 70;
1339        } elseif ($v >= 1300) {
1340            $add = 60;
1341        } elseif ($v >= 1200) {
1342            $add = 60;
1343        } elseif ($v >= 1100) {
1344            $add = 50;
1345        } elseif ($v >= 1000) {
1346            $add = 50;
1347        } else {
1348            $add = 0;
1349        }
1350
1351        return $v + $add;
1352    }
1353
1354    public static function getTsumegoElo($rank, $p = null) {
1355        if ($p != null) {
1356            $p *= 100;
1357        } else {
1358            $p = 0;
1359        }
1360        $elo = 600;
1361        if ($rank == '9d') {
1362            $elo = round(2900 + $p);
1363        } elseif ($rank == '8d') {
1364            $elo = round(2800 + $p);
1365        } elseif ($rank == '7d') {
1366            $elo = round(2700 + $p);
1367        } elseif ($rank == '6d') {
1368            $elo = round(2600 + $p);
1369        } elseif ($rank == '5d') {
1370            $elo = round(2500 + $p);
1371        } elseif ($rank == '4d') {
1372            $elo = round(2400 + $p);
1373        } elseif ($rank == '3d') {
1374            $elo = round(2300 + $p);
1375        } elseif ($rank == '2d') {
1376            $elo = round(2200 + $p);
1377        } elseif ($rank == '1d') {
1378            $elo = round(2100 + $p);
1379        } elseif ($rank == '1k') {
1380            $elo = round(2000 + $p);
1381        } elseif ($rank == '2k') {
1382            $elo = round(1900 + $p);
1383        } elseif ($rank == '3k') {
1384            $elo = round(1800 + $p);
1385        } elseif ($rank == '4k') {
1386            $elo = round(1700 + $p);
1387        } elseif ($rank == '5k') {
1388            $elo = round(1600 + $p);
1389        } elseif ($rank == '6k') {
1390            $elo = round(1500 + $p);
1391        } elseif ($rank == '7k') {
1392            $elo = round(1400 + $p);
1393        } elseif ($rank == '8k') {
1394            $elo = round(1300 + $p);
1395        } elseif ($rank == '9k') {
1396            $elo = round(1200 + $p);
1397        } elseif ($rank == '10k') {
1398            $elo = round(1100 + $p);
1399        } elseif ($rank == '11k') {
1400            $elo = round(1000 + $p);
1401        } elseif ($rank == '12k') {
1402            $elo = round(900 + $p);
1403        } elseif ($rank == '13k') {
1404            $elo = round(800 + $p);
1405        } elseif ($rank == '14k') {
1406            $elo = round(700 + $p);
1407        } elseif ($rank == '15k' || $rank == '16k' || $rank == '17k' || $rank == '18k' || $rank == '19k' || $rank == '20k' || $rank == '21k') {
1408            $elo = round(600 + $p);
1409        }
1410
1411        return $elo;
1412    }
1413    protected function getTsumegoRankVal($t) {
1414        if ($t <= 0) {
1415            return 0;
1416        }
1417        if ($t > 0 && $t <= 22) {
1418            return 22 - $t;
1419        }
1420        if ($t <= 26.5) {
1421            return 26.5 - $t;
1422        }
1423        if ($t <= 30) {
1424            return 30 - $t;
1425        }
1426        if ($t <= 34) {
1427            return 34 - $t;
1428        }
1429        if ($t <= 38) {
1430            return 38 - $t;
1431        }
1432        if ($t <= 42) {
1433            return 42 - $t;
1434        }
1435        if ($t <= 46) {
1436            return 46 - $t;
1437        }
1438        if ($t <= 50) {
1439            return 50 - $t;
1440        }
1441        if ($t <= 54.5) {
1442            return 54.5 - $t;
1443        }
1444        if ($t <= 58.5) {
1445            return 58.5 - $t;
1446        }
1447        if ($t <= 63) {
1448            return 63 - $t;
1449        }
1450        if ($t <= 67) {
1451            return 67 - $t;
1452        }
1453        if ($t <= 70.8) {
1454            return 70.8 - $t;
1455        }
1456        if ($t <= 74.8) {
1457            return 74.8 - $t;
1458        }
1459        if ($t <= 79) {
1460            return 79 - $t;
1461        }
1462        if ($t <= 83.5) {
1463            return 83.5 - $t;
1464        }
1465        if ($t <= 88) {
1466            return 88 - $t;
1467        }
1468        if ($t <= 92) {
1469            return 92 - $t;
1470        }
1471        if ($t <= 96) {
1472            return 96 - $t;
1473        }
1474
1475        return 100 - $t;
1476    }
1477    protected function getTsumegoRankMax($t) {
1478        if ($t <= 0) {
1479            return 100 - 96;
1480        }
1481        if ($t > 0 && $t <= 22) {
1482            return 22;
1483        }
1484        if ($t <= 26.5) {
1485            return 26.5 - 22;
1486        }
1487        if ($t <= 30) {
1488            return 30 - 26.5;
1489        }
1490        if ($t <= 34) {
1491            return 34 - 30;
1492        }
1493        if ($t <= 38) {
1494            return 38 - 34;
1495        }
1496        if ($t <= 42) {
1497            return 42 - 38;
1498        }
1499        if ($t <= 46) {
1500            return 46 - 42;
1501        }
1502        if ($t <= 50) {
1503            return 50 - 46;
1504        }
1505        if ($t <= 54.5) {
1506            return 54.5 - 50;
1507        }
1508        if ($t <= 58.5) {
1509            return 58.5 - 54.5;
1510        }
1511        if ($t <= 63) {
1512            return 63 - 58.5;
1513        }
1514        if ($t <= 67) {
1515            return 67 - 63;
1516        }
1517        if ($t <= 70.8) {
1518            return 70.8 - 67;
1519        }
1520        if ($t <= 74.8) {
1521            return 74.8 - 70.8;
1522        }
1523        if ($t <= 79) {
1524            return 79 - 74.8;
1525        }
1526        if ($t <= 83.5) {
1527            return 83.5 - 79;
1528        }
1529        if ($t <= 88) {
1530            return 88 - 83.5;
1531        }
1532        if ($t <= 92) {
1533            return 92 - 88;
1534        }
1535        if ($t <= 96) {
1536            return 96 - 92;
1537        }
1538
1539        return 100 - 96;
1540    }
1541
1542    public static function saveDanSolveCondition($solvedTsumegoRank, $tId): void {
1543        if ($solvedTsumegoRank == '1d' || $solvedTsumegoRank == '2d' || $solvedTsumegoRank == '3d' || $solvedTsumegoRank == '4d' || $solvedTsumegoRank == '5d') {
1544            $danSolveCategory = 'danSolve' . $solvedTsumegoRank;
1545            $danSolveCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1546                'order' => 'value DESC',
1547                'conditions' => [
1548                    'user_id' => Auth::getUserID(),
1549                    'category' => $danSolveCategory,
1550                ],
1551            ]);
1552            if (!$danSolveCondition) {
1553                $danSolveCondition = [];
1554                $danSolveCondition['AchievementCondition']['value'] = 0;
1555                ClassRegistry::init('AchievementCondition')->create();
1556            }
1557            $danSolveCondition['AchievementCondition']['category'] = $danSolveCategory;
1558            $danSolveCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1559            $danSolveCondition['AchievementCondition']['set_id'] = $tId;
1560            $danSolveCondition['AchievementCondition']['value']++;
1561
1562            ClassRegistry::init('AchievementCondition')->save($danSolveCondition);
1563        }
1564    }
1565
1566    public static function updateSprintCondition(bool $trigger = false): void {
1567        if (Auth::isLoggedIn()) {
1568            $sprintCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1569                'order' => 'value DESC',
1570                'conditions' => [
1571                    'user_id' => Auth::getUserID(),
1572                    'category' => 'sprint',
1573                ],
1574            ]);
1575            if (!$sprintCondition) {
1576                $sprintCondition = [];
1577                $sprintCondition['AchievementCondition']['value'] = 0;
1578                ClassRegistry::init('AchievementCondition')->create();
1579            }
1580            $sprintCondition['AchievementCondition']['category'] = 'sprint';
1581            $sprintCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1582            if ($trigger) {
1583                $sprintCondition['AchievementCondition']['value']++;
1584            } else {
1585                $sprintCondition['AchievementCondition']['value'] = 0;
1586            }
1587            ClassRegistry::init('AchievementCondition')->save($sprintCondition);
1588        }
1589    }
1590
1591    public static function updateGoldenCondition(bool $trigger = false): void {
1592        $goldenCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1593            'order' => 'value DESC',
1594            'conditions' => [
1595                'user_id' => Auth::getUserID(),
1596                'category' => 'golden',
1597            ],
1598        ]);
1599        if (!$goldenCondition) {
1600            $goldenCondition = [];
1601            $goldenCondition['AchievementCondition']['value'] = 0;
1602            ClassRegistry::init('AchievementCondition')->create();
1603        }
1604        $goldenCondition['AchievementCondition']['category'] = 'golden';
1605        $goldenCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1606        if ($trigger) {
1607            $goldenCondition['AchievementCondition']['value']++;
1608        } else {
1609            $goldenCondition['AchievementCondition']['value'] = 0;
1610        }
1611        ClassRegistry::init('AchievementCondition')->save($goldenCondition);
1612    }
1613
1614    public static function setPotionCondition(): void {
1615        $potionCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1616            'order' => 'value DESC',
1617            'conditions' => [
1618                'user_id' => Auth::getUserID(),
1619                'category' => 'potion',
1620            ],
1621        ]);
1622        if (!$potionCondition) {
1623            $potionCondition = [];
1624            ClassRegistry::init('AchievementCondition')->create();
1625        }
1626        $potionCondition['AchievementCondition']['category'] = 'potion';
1627        $potionCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1628        $potionCondition['AchievementCondition']['value'] = 1;
1629        ClassRegistry::init('AchievementCondition')->save($potionCondition);
1630    }
1631
1632    public static function updateGems(string $rank): void {
1633        $datex = new DateTime('today');
1634        $dateGem = ClassRegistry::init('DayRecord')->find('first', ['conditions' => ['date' => $datex->format('Y-m-d')]]);
1635        if ($dateGem != null) {
1636            $gems = explode('-', $dateGem['DayRecord']['gems']);
1637            $gemValue = '';
1638            $gemValue2 = '';
1639            $gemValue3 = '';
1640            $condition1 = 500;
1641            $condition2 = 200;
1642            $condition3 = 5;
1643            $found1 = false;
1644            $found2 = false;
1645            $found3 = false;
1646            if ($rank == '15k' || $rank == '14k' || $rank == '13k' || $rank == '12k' || $rank == '11k' || $rank == '10k') {
1647                if ($gems[0] == 0) {
1648                    $gemValue = '15k';
1649                } elseif ($gems[0] == 1) {
1650                    $gemValue = '12k';
1651                } elseif ($gems[0] == 2) {
1652                    $gemValue = '10k';
1653                }
1654                if ($rank == $gemValue) {
1655                    $dateGem['DayRecord']['gemCounter1']++;
1656                    if ($dateGem['DayRecord']['gemCounter1'] == $condition1) {
1657                        $found1 = true;
1658                    }
1659                }
1660            } elseif ($rank == '9k' || $rank == '8k' || $rank == '7k' || $rank == '6k' || $rank == '5k' || $rank == '4k' || $rank == '3k' || $rank == '2k' || $rank == '1k') {
1661                if ($gems[1] == 0) {
1662                    $gemValue = '9k';
1663                    $gemValue2 = 'x';
1664                    $gemValue3 = 'y';
1665                } elseif ($gems[1] == 1) {
1666                    $gemValue = '6k';
1667                    $gemValue2 = '5k';
1668                    $gemValue3 = '4k';
1669                } elseif ($gems[1] == 2) {
1670                    $gemValue = 'x';
1671                    $gemValue2 = '2k';
1672                    $gemValue3 = '1k';
1673                }
1674                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
1675                    $dateGem['DayRecord']['gemCounter2']++;
1676                    if ($dateGem['DayRecord']['gemCounter2'] == $condition2) {
1677                        $found2 = true;
1678                    }
1679                }
1680            } elseif ($rank == '1d' || $rank == '2d' || $rank == '3d' || $rank == '4d' || $rank == '5d' || $rank == '6d' || $rank == '7d') {
1681                if ($gems[2] == 0) {
1682                    $gemValue = '1d';
1683                    $gemValue2 = '2d';
1684                    $gemValue3 = '3d';
1685                } elseif ($gems[2] == 1) {
1686                    $gemValue = '2d';
1687                    $gemValue2 = '3d';
1688                    $gemValue3 = '4d';
1689                } elseif ($gems[2] == 2) {
1690                    $gemValue = '5d';
1691                    $gemValue2 = '6d';
1692                    $gemValue3 = '7d';
1693                }
1694                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
1695                    $dateGem['DayRecord']['gemCounter3']++;
1696                    if ($dateGem['DayRecord']['gemCounter3'] == $condition3) {
1697                        $found3 = true;
1698                    }
1699                }
1700            }
1701            if ($found1) {
1702                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1703                    'order' => 'value DESC',
1704                    'conditions' => [
1705                        'user_id' => Auth::getUserID(),
1706                        'category' => 'emerald',
1707                    ],
1708                ]);
1709                if ($aCondition == null) {
1710                    $aCondition = [];
1711                    $aCondition['AchievementCondition']['category'] = 'emerald';
1712                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1713                    $aCondition['AchievementCondition']['value'] = 1;
1714                    ClassRegistry::init('AchievementCondition')->save($aCondition);
1715                } else {
1716                    $dateGem['DayRecord']['gemCounter1']--;
1717                }
1718            } elseif ($found2) {
1719                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1720                    'order' => 'value DESC',
1721                    'conditions' => [
1722                        'user_id' => Auth::getUserID(),
1723                        'category' => 'sapphire',
1724                    ],
1725                ]);
1726                if ($aCondition == null) {
1727                    $aCondition = [];
1728                    $aCondition['AchievementCondition']['category'] = 'sapphire';
1729                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1730                    $aCondition['AchievementCondition']['value'] = 1;
1731                    ClassRegistry::init('AchievementCondition')->save($aCondition);
1732                } else {
1733                    $dateGem['DayRecord']['gemCounter2']--;
1734                }
1735            } elseif ($found3) {
1736                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
1737                    'order' => 'value DESC',
1738                    'conditions' => [
1739                        'user_id' => Auth::getUserID(),
1740                        'category' => 'ruby',
1741                    ],
1742                ]);
1743                if ($aCondition == null) {
1744                    $aCondition = [];
1745                    $aCondition['AchievementCondition']['category'] = 'ruby';
1746                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
1747                    $aCondition['AchievementCondition']['value'] = 1;
1748                    ClassRegistry::init('AchievementCondition')->save($aCondition);
1749                } else {
1750                    $dateGem['DayRecord']['gemCounter3']--;
1751                }
1752            }
1753        }
1754        ClassRegistry::init('DayRecord')->save($dateGem);
1755    }
1756
1757    public static function checkProblemNumberAchievements() {
1758        if (!Auth::isLoggedIn()) {
1759            return;
1760        }
1761
1762        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1763        if (!$buffer) {
1764            $buffer = [];
1765        }
1766        $existingAs = [];
1767        $bufferCount = count($buffer);
1768        for ($i = 0; $i < $bufferCount; $i++) {
1769            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1770        }
1771        $as = [];
1772        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1773        $updated = [];
1774
1775        $achievementId = 1;
1776        $solvedCount = Auth::getUser()['solved'];
1777        if ($solvedCount >= 1000 && !isset($existingAs[$achievementId])) {
1778            $as['AchievementStatus']['achievement_id'] = $achievementId;
1779            ClassRegistry::init('AchievementStatus')->create();
1780            ClassRegistry::init('AchievementStatus')->save($as);
1781            array_push($updated, $achievementId);
1782        }
1783        $achievementId = 2;
1784        if ($solvedCount >= 2000 && !isset($existingAs[$achievementId])) {
1785            $as['AchievementStatus']['achievement_id'] = $achievementId;
1786            ClassRegistry::init('AchievementStatus')->create();
1787            ClassRegistry::init('AchievementStatus')->save($as);
1788            array_push($updated, $achievementId);
1789        }
1790        $achievementId = 3;
1791        if ($solvedCount >= 3000 && !isset($existingAs[$achievementId])) {
1792            $as['AchievementStatus']['achievement_id'] = $achievementId;
1793            ClassRegistry::init('AchievementStatus')->create();
1794            ClassRegistry::init('AchievementStatus')->save($as);
1795            array_push($updated, $achievementId);
1796        }
1797        $achievementId = 4;
1798        if ($solvedCount >= 4000 && !isset($existingAs[$achievementId])) {
1799            $as['AchievementStatus']['achievement_id'] = $achievementId;
1800            ClassRegistry::init('AchievementStatus')->create();
1801            ClassRegistry::init('AchievementStatus')->save($as);
1802            array_push($updated, $achievementId);
1803        }
1804        $achievementId = 5;
1805        if ($solvedCount >= 5000 && !isset($existingAs[$achievementId])) {
1806            $as['AchievementStatus']['achievement_id'] = $achievementId;
1807            ClassRegistry::init('AchievementStatus')->create();
1808            ClassRegistry::init('AchievementStatus')->save($as);
1809            array_push($updated, $achievementId);
1810        }
1811        $achievementId = 6;
1812        if ($solvedCount >= 6000 && !isset($existingAs[$achievementId])) {
1813            $as['AchievementStatus']['achievement_id'] = $achievementId;
1814            ClassRegistry::init('AchievementStatus')->create();
1815            ClassRegistry::init('AchievementStatus')->save($as);
1816            array_push($updated, $achievementId);
1817        }
1818        $achievementId = 7;
1819        if ($solvedCount >= 7000 && !isset($existingAs[$achievementId])) {
1820            $as['AchievementStatus']['achievement_id'] = $achievementId;
1821            ClassRegistry::init('AchievementStatus')->create();
1822            ClassRegistry::init('AchievementStatus')->save($as);
1823            array_push($updated, $achievementId);
1824        }
1825        $achievementId = 8;
1826        if ($solvedCount >= 8000 && !isset($existingAs[$achievementId])) {
1827            $as['AchievementStatus']['achievement_id'] = $achievementId;
1828            ClassRegistry::init('AchievementStatus')->create();
1829            ClassRegistry::init('AchievementStatus')->save($as);
1830            array_push($updated, $achievementId);
1831        }
1832        $achievementId = 9;
1833        if ($solvedCount >= 9000 && !isset($existingAs[$achievementId])) {
1834            $as['AchievementStatus']['achievement_id'] = $achievementId;
1835            ClassRegistry::init('AchievementStatus')->create();
1836            ClassRegistry::init('AchievementStatus')->save($as);
1837            array_push($updated, $achievementId);
1838        }
1839        $achievementId = 10;
1840        if ($solvedCount >= 10000 && !isset($existingAs[$achievementId])) {
1841            $as['AchievementStatus']['achievement_id'] = $achievementId;
1842            ClassRegistry::init('AchievementStatus')->create();
1843            ClassRegistry::init('AchievementStatus')->save($as);
1844            array_push($updated, $achievementId);
1845        }
1846        //uotd achievement
1847        $achievementId = 11;
1848        if (!isset($existingAs[$achievementId])) {
1849            $condition = ClassRegistry::init('AchievementCondition')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => 'uotd']]);
1850            if ($condition != null) {
1851                $as['AchievementStatus']['achievement_id'] = $achievementId;
1852                ClassRegistry::init('AchievementStatus')->create();
1853                ClassRegistry::init('AchievementStatus')->save($as);
1854                array_push($updated, $achievementId);
1855            }
1856        }
1857
1858        $updatedCount = count($updated);
1859        for ($i = 0; $i < $updatedCount; $i++) {
1860            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1861            $updated[$i] = [];
1862            $updated[$i][0] = $a['Achievement']['name'];
1863            $updated[$i][1] = $a['Achievement']['description'];
1864            $updated[$i][2] = $a['Achievement']['image'];
1865            $updated[$i][3] = $a['Achievement']['color'];
1866            $updated[$i][4] = $a['Achievement']['xp'];
1867            $updated[$i][5] = $a['Achievement']['id'];
1868        }
1869
1870        return $updated;
1871    }
1872
1873    public static function checkDanSolveAchievements() {
1874        if (Auth::isLoggedIn()) {
1875            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1876            if (!$buffer) {
1877                $buffer = [];
1878            }
1879            $ac = ClassRegistry::init('AchievementCondition')->find('all', [
1880                'order' => 'category ASC',
1881                'conditions' => [
1882                    'user_id' => Auth::getUserID(),
1883                    'OR' => [
1884                        ['category' => 'danSolve1d'],
1885                        ['category' => 'danSolve2d'],
1886                        ['category' => 'danSolve3d'],
1887                        ['category' => 'danSolve4d'],
1888                        ['category' => 'danSolve5d'],
1889                        ['category' => 'emerald'],
1890                        ['category' => 'sapphire'],
1891                        ['category' => 'ruby'],
1892                        ['category' => 'sprint'],
1893                        ['category' => 'golden'],
1894                        ['category' => 'potion'],
1895                    ],
1896                ],
1897            ]);
1898            if (!$ac) {
1899                $ac = [];
1900            }
1901            $ac1 = [];
1902            $acCount = count($ac);
1903            for ($i = 0; $i < $acCount; $i++) {
1904                if ($ac[$i]['AchievementCondition']['category'] == 'danSolve1d') {
1905                    $ac1['1d'] = $ac[$i]['AchievementCondition']['value'];
1906                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve2d') {
1907                    $ac1['2d'] = $ac[$i]['AchievementCondition']['value'];
1908                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve3d') {
1909                    $ac1['3d'] = $ac[$i]['AchievementCondition']['value'];
1910                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve4d') {
1911                    $ac1['4d'] = $ac[$i]['AchievementCondition']['value'];
1912                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve5d') {
1913                    $ac1['5d'] = $ac[$i]['AchievementCondition']['value'];
1914                } elseif ($ac[$i]['AchievementCondition']['category'] == 'emerald') {
1915                    $ac1['emerald'] = $ac[$i]['AchievementCondition']['value'];
1916                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sapphire') {
1917                    $ac1['sapphire'] = $ac[$i]['AchievementCondition']['value'];
1918                } elseif ($ac[$i]['AchievementCondition']['category'] == 'ruby') {
1919                    $ac1['ruby'] = $ac[$i]['AchievementCondition']['value'];
1920                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sprint') {
1921                    $ac1['sprint'] = $ac[$i]['AchievementCondition']['value'];
1922                } elseif ($ac[$i]['AchievementCondition']['category'] == 'golden') {
1923                    $ac1['golden'] = $ac[$i]['AchievementCondition']['value'];
1924                } elseif ($ac[$i]['AchievementCondition']['category'] == 'potion') {
1925                    $ac1['potion'] = $ac[$i]['AchievementCondition']['value'];
1926                }
1927            }
1928
1929            $existingAs = [];
1930            $bufferCount = count($buffer);
1931            for ($i = 0; $i < $bufferCount; $i++) {
1932                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1933            }
1934            $as = [];
1935            $as['AchievementStatus']['user_id'] = Auth::getUserID();
1936            $updated = [];
1937            $achievementId = 101;
1938            if ($ac1['1d'] > 0 && !isset($existingAs[$achievementId])) {
1939                $as['AchievementStatus']['achievement_id'] = $achievementId;
1940                ClassRegistry::init('AchievementStatus')->create();
1941                ClassRegistry::init('AchievementStatus')->save($as);
1942                array_push($updated, $achievementId);
1943            }
1944            $achievementId = 102;
1945            if ($ac1['2d'] > 0 && !isset($existingAs[$achievementId])) {
1946                $as['AchievementStatus']['achievement_id'] = $achievementId;
1947                ClassRegistry::init('AchievementStatus')->create();
1948                ClassRegistry::init('AchievementStatus')->save($as);
1949                array_push($updated, $achievementId);
1950            }
1951            $achievementId = 103;
1952            if ($ac1['3d'] > 0 && !isset($existingAs[$achievementId])) {
1953                $as['AchievementStatus']['achievement_id'] = $achievementId;
1954                ClassRegistry::init('AchievementStatus')->create();
1955                ClassRegistry::init('AchievementStatus')->save($as);
1956                array_push($updated, $achievementId);
1957            }
1958            $achievementId = 104;
1959            if ($ac1['4d'] > 0 && !isset($existingAs[$achievementId])) {
1960                $as['AchievementStatus']['achievement_id'] = $achievementId;
1961                ClassRegistry::init('AchievementStatus')->create();
1962                ClassRegistry::init('AchievementStatus')->save($as);
1963                array_push($updated, $achievementId);
1964            }
1965            $achievementId = 105;
1966            if ($ac1['5d'] > 0 && !isset($existingAs[$achievementId])) {
1967                $as['AchievementStatus']['achievement_id'] = $achievementId;
1968                ClassRegistry::init('AchievementStatus')->create();
1969                ClassRegistry::init('AchievementStatus')->save($as);
1970                array_push($updated, $achievementId);
1971            }
1972            $achievementId = 106;
1973            if ($ac1['1d'] >= 10 && !isset($existingAs[$achievementId])) {
1974                $as['AchievementStatus']['achievement_id'] = $achievementId;
1975                ClassRegistry::init('AchievementStatus')->create();
1976                ClassRegistry::init('AchievementStatus')->save($as);
1977                array_push($updated, $achievementId);
1978            }
1979            $achievementId = 107;
1980            if ($ac1['2d'] >= 10 && !isset($existingAs[$achievementId])) {
1981                $as['AchievementStatus']['achievement_id'] = $achievementId;
1982                ClassRegistry::init('AchievementStatus')->create();
1983                ClassRegistry::init('AchievementStatus')->save($as);
1984                array_push($updated, $achievementId);
1985            }
1986            $achievementId = 108;
1987            if ($ac1['3d'] >= 10 && !isset($existingAs[$achievementId])) {
1988                $as['AchievementStatus']['achievement_id'] = $achievementId;
1989                ClassRegistry::init('AchievementStatus')->create();
1990                ClassRegistry::init('AchievementStatus')->save($as);
1991                array_push($updated, $achievementId);
1992            }
1993            $achievementId = 109;
1994            if ($ac1['4d'] >= 10 && !isset($existingAs[$achievementId])) {
1995                $as['AchievementStatus']['achievement_id'] = $achievementId;
1996                ClassRegistry::init('AchievementStatus')->create();
1997                ClassRegistry::init('AchievementStatus')->save($as);
1998                array_push($updated, $achievementId);
1999            }
2000            $achievementId = 110;
2001            if ($ac1['5d'] >= 10 && !isset($existingAs[$achievementId])) {
2002                $as['AchievementStatus']['achievement_id'] = $achievementId;
2003                ClassRegistry::init('AchievementStatus')->create();
2004                ClassRegistry::init('AchievementStatus')->save($as);
2005                array_push($updated, $achievementId);
2006            }
2007            $achievementId = 111;
2008            if (isset($ac1['emerald'])) {
2009                if ($ac1['emerald'] == 1 && !isset($existingAs[$achievementId])) {
2010                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2011                    ClassRegistry::init('AchievementStatus')->create();
2012                    ClassRegistry::init('AchievementStatus')->save($as);
2013                    array_push($updated, $achievementId);
2014                }
2015            }
2016            $achievementId = 112;
2017            if (isset($ac1['sapphire'])) {
2018                if ($ac1['sapphire'] == 1 && !isset($existingAs[$achievementId])) {
2019                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2020                    ClassRegistry::init('AchievementStatus')->create();
2021                    ClassRegistry::init('AchievementStatus')->save($as);
2022                    array_push($updated, $achievementId);
2023                }
2024            }
2025            $achievementId = 113;
2026            if (isset($ac1['ruby'])) {
2027                if ($ac1['ruby'] == 1 && !isset($existingAs[$achievementId])) {
2028                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2029                    ClassRegistry::init('AchievementStatus')->create();
2030                    ClassRegistry::init('AchievementStatus')->save($as);
2031                    array_push($updated, $achievementId);
2032                }
2033            }
2034            $achievementId = 114;
2035            if (!isset($existingAs[$achievementId]) && isset($existingAs[111]) && isset($existingAs[112]) && isset($existingAs[113])) {
2036                $as['AchievementStatus']['achievement_id'] = $achievementId;
2037                ClassRegistry::init('AchievementStatus')->create();
2038                ClassRegistry::init('AchievementStatus')->save($as);
2039                array_push($updated, $achievementId);
2040            }
2041            $achievementId = 96;
2042            if (!isset($existingAs[$achievementId]) && $ac1['sprint'] >= 30) {
2043                $as['AchievementStatus']['achievement_id'] = $achievementId;
2044                ClassRegistry::init('AchievementStatus')->create();
2045                ClassRegistry::init('AchievementStatus')->save($as);
2046                array_push($updated, $achievementId);
2047            }
2048            $achievementId = 97;
2049            if (!isset($existingAs[$achievementId]) && $ac1['golden'] >= 10) {
2050                $as['AchievementStatus']['achievement_id'] = $achievementId;
2051                ClassRegistry::init('AchievementStatus')->create();
2052                ClassRegistry::init('AchievementStatus')->save($as);
2053                array_push($updated, $achievementId);
2054            }
2055            $achievementId = 98;
2056            if (!isset($existingAs[$achievementId]) && $ac1['potion'] >= 1) {
2057                $as['AchievementStatus']['achievement_id'] = $achievementId;
2058                ClassRegistry::init('AchievementStatus')->create();
2059                ClassRegistry::init('AchievementStatus')->save($as);
2060                array_push($updated, $achievementId);
2061            }
2062            $updatedCount = count($updated);
2063            for ($i = 0; $i < $updatedCount; $i++) {
2064                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2065                $updated[$i] = [];
2066                $updated[$i][0] = $a['Achievement']['name'];
2067                $updated[$i][1] = $a['Achievement']['description'];
2068                $updated[$i][2] = $a['Achievement']['image'];
2069                $updated[$i][3] = $a['Achievement']['color'];
2070                $updated[$i][4] = $a['Achievement']['xp'];
2071                $updated[$i][5] = $a['Achievement']['id'];
2072            }
2073
2074            return $updated;
2075        }
2076    }
2077
2078    protected function checkForLocked($t, $setsWithPremium) {
2079        $scCheck = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
2080        if ($scCheck && in_array($scCheck['SetConnection']['set_id'], $setsWithPremium) && !Auth::hasPremium()) {
2081            $t['Tsumego']['locked'] = true;
2082        } else {
2083            $t['Tsumego']['locked'] = false;
2084        }
2085
2086        return $t;
2087    }
2088    public static function checkNoErrorAchievements() {
2089        if (Auth::isLoggedIn()) {
2090
2091            $ac = ClassRegistry::init('AchievementCondition')->find('first', [
2092                'order' => 'value DESC',
2093                'conditions' => [
2094                    'user_id' => Auth::getUserID(),
2095                    'category' => 'err',
2096                ],
2097            ]);
2098
2099            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2100            if (!$buffer) {
2101                $buffer = [];
2102            }
2103            $existingAs = [];
2104            $bufferCount = count($buffer);
2105            for ($i = 0; $i < $bufferCount; $i++) {
2106                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2107            }
2108            $as = [];
2109            $as['AchievementStatus']['user_id'] = Auth::getUserID();
2110            $updated = [];
2111
2112            $achievementId = 53;
2113            if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
2114                $as['AchievementStatus']['achievement_id'] = $achievementId;
2115                ClassRegistry::init('AchievementStatus')->create();
2116                ClassRegistry::init('AchievementStatus')->save($as);
2117                array_push($updated, $achievementId);
2118            }
2119            $achievementId = 54;
2120            if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
2121                $as['AchievementStatus']['achievement_id'] = $achievementId;
2122                ClassRegistry::init('AchievementStatus')->create();
2123                ClassRegistry::init('AchievementStatus')->save($as);
2124                array_push($updated, $achievementId);
2125            }
2126            $achievementId = 55;
2127            if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
2128                $as['AchievementStatus']['achievement_id'] = $achievementId;
2129                ClassRegistry::init('AchievementStatus')->create();
2130                ClassRegistry::init('AchievementStatus')->save($as);
2131                array_push($updated, $achievementId);
2132            }
2133            $achievementId = 56;
2134            if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
2135                $as['AchievementStatus']['achievement_id'] = $achievementId;
2136                ClassRegistry::init('AchievementStatus')->create();
2137                ClassRegistry::init('AchievementStatus')->save($as);
2138                array_push($updated, $achievementId);
2139            }
2140            $achievementId = 57;
2141            if ($ac['AchievementCondition']['value'] >= 100 && !isset($existingAs[$achievementId])) {
2142                $as['AchievementStatus']['achievement_id'] = $achievementId;
2143                ClassRegistry::init('AchievementStatus')->create();
2144                ClassRegistry::init('AchievementStatus')->save($as);
2145                array_push($updated, $achievementId);
2146            }
2147            $achievementId = 58;
2148            if ($ac['AchievementCondition']['value'] >= 200 && !isset($existingAs[$achievementId])) {
2149                $as['AchievementStatus']['achievement_id'] = $achievementId;
2150                ClassRegistry::init('AchievementStatus')->create();
2151                ClassRegistry::init('AchievementStatus')->save($as);
2152                array_push($updated, $achievementId);
2153            }
2154            $updatedCount = count($updated);
2155            for ($i = 0; $i < $updatedCount; $i++) {
2156                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2157                $updated[$i] = [];
2158                $updated[$i][0] = $a['Achievement']['name'];
2159                $updated[$i][1] = $a['Achievement']['description'];
2160                $updated[$i][2] = $a['Achievement']['image'];
2161                $updated[$i][3] = $a['Achievement']['color'];
2162                $updated[$i][4] = $a['Achievement']['xp'];
2163                $updated[$i][5] = $a['Achievement']['id'];
2164            }
2165
2166            return $updated;
2167        }
2168    }
2169
2170    protected function checkTimeModeAchievements() {
2171        $this->loadModel('Achievement');
2172        $this->loadModel('AchievementStatus');
2173        $this->loadModel('TimeModeSession');
2174
2175        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2176        if (!$buffer) {
2177            $buffer = [];
2178        }
2179        $existingAs = [];
2180        $bufferCount = count($buffer);
2181        for ($i = 0; $i < $bufferCount; $i++) {
2182            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2183        }
2184        $as = [];
2185        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2186        $updated = [];
2187
2188        $rBlitz = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_BLITZ, 'user_id' => Auth::getUserID()]]);
2189        if (!$rBlitz) {
2190            $rBlitz = [];
2191        }
2192        $rFast = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_FAST_SPEED, 'user_id' => Auth::getUserID()]]);
2193        if (!$rFast) {
2194            $rFast = [];
2195        }
2196        $rSlow = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_SLOW_SPEED, 'user_id' => Auth::getUserID()]]);
2197        if (!$rSlow) {
2198            $rSlow = [];
2199        }
2200        $r = $this->TimeModeSession->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2201        if (!$r) {
2202            $r = [];
2203        }
2204
2205        $timeModeAchievements = [];
2206        for ($i = 70; $i <= 91; $i++) {
2207            $timeModeAchievements[$i] = false;
2208        }
2209        $rCount = count($r);
2210        for ($i = 0; $i < $rCount; $i++) {
2211            if ($r[$i]['TimeModeSession']['status'] == 's') {
2212                if ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k') {
2213                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2214                        $timeModeAchievements[70] = true;
2215                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2216                        $timeModeAchievements[76] = true;
2217                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2218                        $timeModeAchievements[82] = true;
2219                    }
2220                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k') {
2221                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2222                        $timeModeAchievements[71] = true;
2223                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2224                        $timeModeAchievements[77] = true;
2225                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2226                        $timeModeAchievements[83] = true;
2227                    }
2228                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k') {
2229                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2230                        $timeModeAchievements[72] = true;
2231                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2232                        $timeModeAchievements[78] = true;
2233                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2234                        $timeModeAchievements[84] = true;
2235                    }
2236                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k') {
2237                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2238                        $timeModeAchievements[73] = true;
2239                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2240                        $timeModeAchievements[79] = true;
2241                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2242                        $timeModeAchievements[85] = true;
2243                    }
2244                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k') {
2245                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2246                        $timeModeAchievements[74] = true;
2247                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2248                        $timeModeAchievements[80] = true;
2249                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2250                        $timeModeAchievements[86] = true;
2251                    }
2252                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d') {
2253                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
2254                        $timeModeAchievements[75] = true;
2255                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
2256                        $timeModeAchievements[81] = true;
2257                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
2258                        $timeModeAchievements[87] = true;
2259                    }
2260                }
2261            }
2262            if ($r[$i]['TimeModeSession']['points'] >= 850
2263            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
2264            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
2265            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d')) {
2266                $timeModeAchievements[91] = true;
2267            }
2268            if ($r[$i]['TimeModeSession']['points'] >= 875
2269            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
2270            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
2271            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k')) {
2272                $timeModeAchievements[90] = true;
2273            }
2274            if ($r[$i]['TimeModeSession']['points'] >= 900
2275            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
2276            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
2277            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
2278            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k')) {
2279                $timeModeAchievements[89] = true;
2280            }
2281            if ($r[$i]['TimeModeSession']['points'] >= 950
2282            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
2283            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
2284            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
2285            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '9k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '10k')) {
2286                $timeModeAchievements[88] = true;
2287            }
2288        }
2289        for ($i = 70; $i <= 91; $i++) {
2290            $achievementId = $i;
2291            if ($timeModeAchievements[$achievementId] == true && !isset($existingAs[$achievementId])) {
2292                $as['AchievementStatus']['achievement_id'] = $achievementId;
2293                ClassRegistry::init('AchievementStatus')->create();
2294                ClassRegistry::init('AchievementStatus')->save($as);
2295                array_push($updated, $achievementId);
2296            }
2297        }
2298        $updatedCount = count($updated);
2299        for ($i = 0; $i < $updatedCount; $i++) {
2300            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2301            $updated[$i] = [];
2302            $updated[$i][0] = $a['Achievement']['name'];
2303            $updated[$i][1] = $a['Achievement']['description'];
2304            $updated[$i][2] = $a['Achievement']['image'];
2305            $updated[$i][3] = $a['Achievement']['color'];
2306            $updated[$i][4] = $a['Achievement']['xp'];
2307            $updated[$i][5] = $a['Achievement']['id'];
2308        }
2309
2310        return $updated;
2311    }
2312
2313    public static function checkRatingAchievements() {
2314        if (!Auth::isLoggedIn()) {
2315            return;
2316        }
2317
2318        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2319        if (!$buffer) {
2320            $buffer = [];
2321        }
2322        $existingAs = [];
2323        $bufferCount = count($buffer);
2324        for ($i = 0; $i < $bufferCount; $i++) {
2325            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2326        }
2327        $as = [];
2328        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2329        $updated = [];
2330
2331        $achievementId = 59;
2332        $currentElo = Auth::getUser()['rating'];
2333        if ($currentElo >= 1500 && !isset($existingAs[$achievementId])) {
2334            $as['AchievementStatus']['achievement_id'] = $achievementId;
2335            ClassRegistry::init('AchievementStatus')->create();
2336            ClassRegistry::init('AchievementStatus')->save($as);
2337            array_push($updated, $achievementId);
2338        }
2339        $achievementId = 60;
2340        if ($currentElo >= 1600 && !isset($existingAs[$achievementId])) {
2341            $as['AchievementStatus']['achievement_id'] = $achievementId;
2342            ClassRegistry::init('AchievementStatus')->create();
2343            ClassRegistry::init('AchievementStatus')->save($as);
2344            array_push($updated, $achievementId);
2345        }
2346        $achievementId = 61;
2347        if ($currentElo >= 1700 && !isset($existingAs[$achievementId])) {
2348            $as['AchievementStatus']['achievement_id'] = $achievementId;
2349            ClassRegistry::init('AchievementStatus')->create();
2350            ClassRegistry::init('AchievementStatus')->save($as);
2351            array_push($updated, $achievementId);
2352        }
2353        $achievementId = 62;
2354        if ($currentElo >= 1800 && !isset($existingAs[$achievementId])) {
2355            $as['AchievementStatus']['achievement_id'] = $achievementId;
2356            ClassRegistry::init('AchievementStatus')->create();
2357            ClassRegistry::init('AchievementStatus')->save($as);
2358            array_push($updated, $achievementId);
2359        }
2360        $achievementId = 63;
2361        if ($currentElo >= 1900 && !isset($existingAs[$achievementId])) {
2362            $as['AchievementStatus']['achievement_id'] = $achievementId;
2363            ClassRegistry::init('AchievementStatus')->create();
2364            ClassRegistry::init('AchievementStatus')->save($as);
2365            array_push($updated, $achievementId);
2366        }
2367        $achievementId = 64;
2368        if ($currentElo >= 2000 && !isset($existingAs[$achievementId])) {
2369            $as['AchievementStatus']['achievement_id'] = $achievementId;
2370            ClassRegistry::init('AchievementStatus')->create();
2371            ClassRegistry::init('AchievementStatus')->save($as);
2372            array_push($updated, $achievementId);
2373        }
2374        $achievementId = 65;
2375        if ($currentElo >= 2100 && !isset($existingAs[$achievementId])) {
2376            $as['AchievementStatus']['achievement_id'] = $achievementId;
2377            ClassRegistry::init('AchievementStatus')->create();
2378            ClassRegistry::init('AchievementStatus')->save($as);
2379            array_push($updated, $achievementId);
2380        }
2381        $achievementId = 66;
2382        if ($currentElo >= 2200 && !isset($existingAs[$achievementId])) {
2383            $as['AchievementStatus']['achievement_id'] = $achievementId;
2384            ClassRegistry::init('AchievementStatus')->create();
2385            ClassRegistry::init('AchievementStatus')->save($as);
2386            array_push($updated, $achievementId);
2387        }
2388        $achievementId = 67;
2389        if ($currentElo >= 2300 && !isset($existingAs[$achievementId])) {
2390            $as['AchievementStatus']['achievement_id'] = $achievementId;
2391            ClassRegistry::init('AchievementStatus')->create();
2392            ClassRegistry::init('AchievementStatus')->save($as);
2393            array_push($updated, $achievementId);
2394        }
2395        $achievementId = 68;
2396        if ($currentElo >= 2400 && !isset($existingAs[$achievementId])) {
2397            $as['AchievementStatus']['achievement_id'] = $achievementId;
2398            ClassRegistry::init('AchievementStatus')->create();
2399            ClassRegistry::init('AchievementStatus')->save($as);
2400            array_push($updated, $achievementId);
2401        }
2402        $achievementId = 69;
2403        if ($currentElo >= 2500 && !isset($existingAs[$achievementId])) {
2404            $as['AchievementStatus']['achievement_id'] = $achievementId;
2405            ClassRegistry::init('AchievementStatus')->create();
2406            ClassRegistry::init('AchievementStatus')->save($as);
2407            array_push($updated, $achievementId);
2408        }
2409        $updatedCount = count($updated);
2410        for ($i = 0; $i < $updatedCount; $i++) {
2411            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2412            $updated[$i] = [];
2413            $updated[$i][0] = $a['Achievement']['name'];
2414            $updated[$i][1] = $a['Achievement']['description'];
2415            $updated[$i][2] = $a['Achievement']['image'];
2416            $updated[$i][3] = $a['Achievement']['color'];
2417            $updated[$i][4] = $a['Achievement']['xp'];
2418            $updated[$i][5] = $a['Achievement']['id'];
2419        }
2420
2421        return $updated;
2422    }
2423
2424    public static function checkLevelAchievements() {
2425        if (!Auth::isLoggedIn()) {
2426            return;
2427        }
2428        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2429        if (!$buffer) {
2430            $buffer = [];
2431        }
2432        $existingAs = [];
2433        $bufferCount = count($buffer);
2434        for ($i = 0; $i < $bufferCount; $i++) {
2435            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2436        }
2437        $as = [];
2438        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2439        $updated = [];
2440
2441        $achievementId = 36;
2442        $userLevel = Auth::getUser()['level'];
2443        if ($userLevel >= 10 && !isset($existingAs[$achievementId])) {
2444            $as['AchievementStatus']['achievement_id'] = $achievementId;
2445            ClassRegistry::init('AchievementStatus')->create();
2446            ClassRegistry::init('AchievementStatus')->save($as);
2447            array_push($updated, $achievementId);
2448        }
2449        $achievementId = 37;
2450        if ($userLevel >= 20 && !isset($existingAs[$achievementId])) {
2451            $as['AchievementStatus']['achievement_id'] = $achievementId;
2452            ClassRegistry::init('AchievementStatus')->create();
2453            ClassRegistry::init('AchievementStatus')->save($as);
2454            array_push($updated, $achievementId);
2455        }
2456        $achievementId = 38;
2457        if ($userLevel >= 30 && !isset($existingAs[$achievementId])) {
2458            $as['AchievementStatus']['achievement_id'] = $achievementId;
2459            ClassRegistry::init('AchievementStatus')->create();
2460            ClassRegistry::init('AchievementStatus')->save($as);
2461            array_push($updated, $achievementId);
2462        }
2463        $achievementId = 39;
2464        if ($userLevel >= 40 && !isset($existingAs[$achievementId])) {
2465            $as['AchievementStatus']['achievement_id'] = $achievementId;
2466            ClassRegistry::init('AchievementStatus')->create();
2467            ClassRegistry::init('AchievementStatus')->save($as);
2468            array_push($updated, $achievementId);
2469        }
2470        $achievementId = 40;
2471        if ($userLevel >= 50 && !isset($existingAs[$achievementId])) {
2472            $as['AchievementStatus']['achievement_id'] = $achievementId;
2473            ClassRegistry::init('AchievementStatus')->create();
2474            ClassRegistry::init('AchievementStatus')->save($as);
2475            array_push($updated, $achievementId);
2476        }
2477        $achievementId = 41;
2478        if ($userLevel >= 60 && !isset($existingAs[$achievementId])) {
2479            $as['AchievementStatus']['achievement_id'] = $achievementId;
2480            ClassRegistry::init('AchievementStatus')->create();
2481            ClassRegistry::init('AchievementStatus')->save($as);
2482            array_push($updated, $achievementId);
2483        }
2484        $achievementId = 42;
2485        if ($userLevel >= 70 && !isset($existingAs[$achievementId])) {
2486            $as['AchievementStatus']['achievement_id'] = $achievementId;
2487            ClassRegistry::init('AchievementStatus')->create();
2488            ClassRegistry::init('AchievementStatus')->save($as);
2489            array_push($updated, $achievementId);
2490        }
2491        $achievementId = 43;
2492        if ($userLevel >= 80 && !isset($existingAs[$achievementId])) {
2493            $as['AchievementStatus']['achievement_id'] = $achievementId;
2494            ClassRegistry::init('AchievementStatus')->create();
2495            ClassRegistry::init('AchievementStatus')->save($as);
2496            array_push($updated, $achievementId);
2497        }
2498        $achievementId = 44;
2499        if ($userLevel >= 90 && !isset($existingAs[$achievementId])) {
2500            $as['AchievementStatus']['achievement_id'] = $achievementId;
2501            ClassRegistry::init('AchievementStatus')->create();
2502            ClassRegistry::init('AchievementStatus')->save($as);
2503            array_push($updated, $achievementId);
2504        }
2505        $achievementId = 45;
2506        if ($userLevel >= 100 && !isset($existingAs[$achievementId])) {
2507            $as['AchievementStatus']['achievement_id'] = $achievementId;
2508            ClassRegistry::init('AchievementStatus')->create();
2509            ClassRegistry::init('AchievementStatus')->save($as);
2510            array_push($updated, $achievementId);
2511        }
2512        $achievementId = 100;
2513        if (Auth::hasPremium() && !isset($existingAs[$achievementId])) {
2514            $as['AchievementStatus']['achievement_id'] = $achievementId;
2515            ClassRegistry::init('AchievementStatus')->create();
2516            ClassRegistry::init('AchievementStatus')->save($as);
2517            array_push($updated, $achievementId);
2518        }
2519        $updatedCount = count($updated);
2520        for ($i = 0; $i < $updatedCount; $i++) {
2521            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2522            $updated[$i] = [];
2523            $updated[$i][0] = $a['Achievement']['name'];
2524            $updated[$i][1] = $a['Achievement']['description'];
2525            $updated[$i][2] = $a['Achievement']['image'];
2526            $updated[$i][3] = $a['Achievement']['color'];
2527            $updated[$i][4] = $a['Achievement']['xp'];
2528            $updated[$i][5] = $a['Achievement']['id'];
2529        }
2530
2531        return $updated;
2532    }
2533
2534    protected function checkSetCompletedAchievements() {
2535        $this->loadModel('Set');
2536        $this->loadModel('Tsumego');
2537        $this->loadModel('Achievement');
2538        $this->loadModel('AchievementStatus');
2539        $this->loadModel('AchievementCondition');
2540
2541        $ac = ClassRegistry::init('AchievementCondition')->find('first', [
2542            'order' => 'value DESC',
2543            'conditions' => [
2544                'user_id' => Auth::getUserID(),
2545                'category' => 'set',
2546            ],
2547        ]);
2548
2549        if (!$ac) {
2550            return [];
2551        }
2552
2553        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2554        if (!$buffer) {
2555            $buffer = [];
2556        }
2557        $existingAs = [];
2558        $bufferCount = count($buffer);
2559        for ($i = 0; $i < $bufferCount; $i++) {
2560            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2561        }
2562        $as = [];
2563        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2564        $updated = [];
2565
2566        $achievementId = 47;
2567        if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
2568            $as['AchievementStatus']['achievement_id'] = $achievementId;
2569            ClassRegistry::init('AchievementStatus')->create();
2570            ClassRegistry::init('AchievementStatus')->save($as);
2571            array_push($updated, $achievementId);
2572        }
2573        $achievementId = 48;
2574        if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
2575            $as['AchievementStatus']['achievement_id'] = $achievementId;
2576            ClassRegistry::init('AchievementStatus')->create();
2577            ClassRegistry::init('AchievementStatus')->save($as);
2578            array_push($updated, $achievementId);
2579        }
2580        $achievementId = 49;
2581        if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
2582            $as['AchievementStatus']['achievement_id'] = $achievementId;
2583            ClassRegistry::init('AchievementStatus')->create();
2584            ClassRegistry::init('AchievementStatus')->save($as);
2585            array_push($updated, $achievementId);
2586        }
2587        $achievementId = 50;
2588        if ($ac['AchievementCondition']['value'] >= 40 && !isset($existingAs[$achievementId])) {
2589            $as['AchievementStatus']['achievement_id'] = $achievementId;
2590            ClassRegistry::init('AchievementStatus')->create();
2591            ClassRegistry::init('AchievementStatus')->save($as);
2592            array_push($updated, $achievementId);
2593        }
2594        $achievementId = 51;
2595        if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
2596            $as['AchievementStatus']['achievement_id'] = $achievementId;
2597            ClassRegistry::init('AchievementStatus')->create();
2598            ClassRegistry::init('AchievementStatus')->save($as);
2599            array_push($updated, $achievementId);
2600        }
2601        $achievementId = 52;
2602        if ($ac['AchievementCondition']['value'] >= 60 && !isset($existingAs[$achievementId])) {
2603            $as['AchievementStatus']['achievement_id'] = $achievementId;
2604            ClassRegistry::init('AchievementStatus')->create();
2605            ClassRegistry::init('AchievementStatus')->save($as);
2606            array_push($updated, $achievementId);
2607        }
2608        $updatedCount = count($updated);
2609        for ($i = 0; $i < $updatedCount; $i++) {
2610            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2611            $updated[$i] = [];
2612            $updated[$i][0] = $a['Achievement']['name'];
2613            $updated[$i][1] = $a['Achievement']['description'];
2614            $updated[$i][2] = $a['Achievement']['image'];
2615            $updated[$i][3] = $a['Achievement']['color'];
2616            $updated[$i][4] = $a['Achievement']['xp'];
2617            $updated[$i][5] = $a['Achievement']['id'];
2618        }
2619
2620        return $updated;
2621    }
2622
2623    protected function setAchievementSpecial($s = null) {
2624        $this->loadModel('Set');
2625        $this->loadModel('Tsumego');
2626        $this->loadModel('TsumegoStatus');
2627        $this->loadModel('Achievement');
2628        $this->loadModel('AchievementStatus');
2629        $this->loadModel('SetConnection');
2630
2631        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2632        if (!$buffer) {
2633            $buffer = [];
2634        }
2635        $existingAs = [];
2636        $bufferCount = count($buffer);
2637        for ($i = 0; $i < $bufferCount; $i++) {
2638            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2639        }
2640        $as = [];
2641        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2642        $updated = [];
2643
2644        $tsIds = [];
2645        $completed = '';
2646        if ($s == 'cc1') {
2647            $ts1 = TsumegoUtil::collectTsumegosFromSet(50);
2648            $ts2 = TsumegoUtil::collectTsumegosFromSet(52);
2649            $ts3 = TsumegoUtil::collectTsumegosFromSet(53);
2650            $ts4 = TsumegoUtil::collectTsumegosFromSet(54);
2651            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
2652            $tsCount = count($ts);
2653            for ($i = 0; $i < $tsCount; $i++) {
2654                array_push($tsIds, $ts[$i]['Tsumego']['id']);
2655            }
2656            $uts = $this->TsumegoStatus->find('all', [
2657                'conditions' => [
2658                    'user_id' => Auth::getUserID(),
2659                    'tsumego_id' => $tsIds,
2660                ],
2661            ]);
2662            if (!$uts) {
2663                $uts = [];
2664            }
2665            $counter = 0;
2666            $utsCount = count($uts);
2667            for ($j = 0; $j < $utsCount; $j++) {
2668                for ($k = 0; $k < $tsCount; $k++) {
2669                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
2670                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
2671                        $counter++;
2672                    }
2673                }
2674            }
2675            if ($counter == count($ts)) {
2676                $completed = $s;
2677            }
2678        } elseif ($s == 'cc2') {
2679            $ts1 = TsumegoUtil::collectTsumegosFromSet(41);
2680            $ts2 = TsumegoUtil::collectTsumegosFromSet(49);
2681            $ts3 = TsumegoUtil::collectTsumegosFromSet(65);
2682            $ts4 = TsumegoUtil::collectTsumegosFromSet(66);
2683            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
2684            $tsCount = count($ts);
2685            for ($i = 0; $i < $tsCount; $i++) {
2686                array_push($tsIds, $ts[$i]['Tsumego']['id']);
2687            }
2688            $uts = $this->TsumegoStatus->find('all', [
2689                'conditions' => [
2690                    'user_id' => Auth::getUserID(),
2691                    'tsumego_id' => $tsIds,
2692                ],
2693            ]);
2694            if (!$uts) {
2695                $uts = [];
2696            }
2697            $counter = 0;
2698            $utsCount = count($uts);
2699            for ($j = 0; $j < $utsCount; $j++) {
2700                for ($k = 0; $k < $tsCount; $k++) {
2701                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
2702                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
2703                        $counter++;
2704                    }
2705                }
2706            }
2707            if ($counter == count($ts)) {
2708                $completed = $s;
2709            }
2710        } elseif ($s == 'cc3') {
2711            $ts1 = TsumegoUtil::collectTsumegosFromSet(186);
2712            $ts2 = TsumegoUtil::collectTsumegosFromSet(187);
2713            $ts3 = TsumegoUtil::collectTsumegosFromSet(196);
2714            $ts4 = TsumegoUtil::collectTsumegosFromSet(203);
2715            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
2716            $tsCount = count($ts);
2717            for ($i = 0; $i < $tsCount; $i++) {
2718                array_push($tsIds, $ts[$i]['Tsumego']['id']);
2719            }
2720            $uts = $this->TsumegoStatus->find('all', [
2721                'conditions' => [
2722                    'user_id' => Auth::getUserID(),
2723                    'tsumego_id' => $tsIds,
2724                ],
2725            ]);
2726            if (!$uts) {
2727                $uts = [];
2728            }
2729            $counter = 0;
2730            $utsCount = count($uts);
2731            for ($j = 0; $j < $utsCount; $j++) {
2732                for ($k = 0; $k < $tsCount; $k++) {
2733                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
2734                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
2735                        $counter++;
2736                    }
2737                }
2738            }
2739            if ($counter == count($ts)) {
2740                $completed = $s;
2741            }
2742        } elseif ($s == '1000w1') {
2743            $ts1 = TsumegoUtil::collectTsumegosFromSet(190);
2744            $ts2 = TsumegoUtil::collectTsumegosFromSet(193);
2745            $ts3 = TsumegoUtil::collectTsumegosFromSet(198);
2746            $ts = array_merge($ts1, $ts2, $ts3);
2747            $tsCount = count($ts);
2748            for ($i = 0; $i < $tsCount; $i++) {
2749                array_push($tsIds, $ts[$i]['Tsumego']['id']);
2750            }
2751            $uts = $this->TsumegoStatus->find('all', [
2752                'conditions' => [
2753                    'user_id' => Auth::getUserID(),
2754                    'tsumego_id' => $tsIds,
2755                ],
2756            ]);
2757            if (!$uts) {
2758                $uts = [];
2759            }
2760            $counter = 0;
2761            $utsCount = count($uts);
2762            for ($j = 0; $j < $utsCount; $j++) {
2763                for ($k = 0; $k < $tsCount; $k++) {
2764                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
2765                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
2766                        $counter++;
2767                    }
2768                }
2769            }
2770            if ($counter == count($ts)) {
2771                $completed = $s;
2772            }
2773        } elseif ($s == '1000w2') {
2774            $ts = TsumegoUtil::collectTsumegosFromSet(216);
2775            $tsCount = count($ts);
2776            for ($i = 0; $i < $tsCount; $i++) {
2777                array_push($tsIds, $ts[$i]['Tsumego']['id']);
2778            }
2779            $uts = $this->TsumegoStatus->find('all', [
2780                'conditions' => [
2781                    'user_id' => Auth::getUserID(),
2782                    'tsumego_id' => $tsIds,
2783                ],
2784            ]);
2785            if (!$uts) {
2786                $uts = [];
2787            }
2788            $counter = 0;
2789            $utsCount = count($uts);
2790            for ($j = 0; $j < $utsCount; $j++) {
2791                for ($k = 0; $k < $tsCount; $k++) {
2792                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
2793                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
2794                        $counter++;
2795                    }
2796                }
2797            }
2798            if ($counter == count($ts)) {
2799                $completed = $s;
2800            }
2801        }
2802
2803        $achievementId = 92;
2804        if ($completed == 'cc1' && !isset($existingAs[$achievementId])) {
2805            $as['AchievementStatus']['achievement_id'] = $achievementId;
2806            ClassRegistry::init('AchievementStatus')->create();
2807            ClassRegistry::init('AchievementStatus')->save($as);
2808            array_push($updated, $achievementId);
2809        }
2810        $achievementId = 93;
2811        if ($completed == 'cc2' && !isset($existingAs[$achievementId])) {
2812            $as['AchievementStatus']['achievement_id'] = $achievementId;
2813            ClassRegistry::init('AchievementStatus')->create();
2814            ClassRegistry::init('AchievementStatus')->save($as);
2815            array_push($updated, $achievementId);
2816        }
2817        $achievementId = 94;
2818        if ($completed == 'cc3' && !isset($existingAs[$achievementId])) {
2819            $as['AchievementStatus']['achievement_id'] = $achievementId;
2820            ClassRegistry::init('AchievementStatus')->create();
2821            ClassRegistry::init('AchievementStatus')->save($as);
2822            array_push($updated, $achievementId);
2823        }
2824        $achievementId = 95;
2825        if ($completed == '1000w1' && !isset($existingAs[$achievementId])) {
2826            $as['AchievementStatus']['achievement_id'] = $achievementId;
2827            ClassRegistry::init('AchievementStatus')->create();
2828            ClassRegistry::init('AchievementStatus')->save($as);
2829            array_push($updated, $achievementId);
2830        }
2831        $achievementId = 115;
2832        if ($completed == '1000w2' && !isset($existingAs[$achievementId])) {
2833            $as['AchievementStatus']['achievement_id'] = $achievementId;
2834            ClassRegistry::init('AchievementStatus')->create();
2835            ClassRegistry::init('AchievementStatus')->save($as);
2836            array_push($updated, $achievementId);
2837        }
2838        $updatedCount = count($updated);
2839        for ($i = 0; $i < $updatedCount; $i++) {
2840            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
2841            $updated[$i] = [];
2842            $updated[$i][0] = $a['Achievement']['name'];
2843            $updated[$i][1] = $a['Achievement']['description'];
2844            $updated[$i][2] = $a['Achievement']['image'];
2845            $updated[$i][3] = $a['Achievement']['color'];
2846            $updated[$i][4] = $a['Achievement']['xp'];
2847            $updated[$i][5] = $a['Achievement']['id'];
2848        }
2849
2850        return $updated;
2851    }
2852
2853    protected function checkSetAchievements($sid = null) {
2854        $this->loadModel('Set');
2855        $this->loadModel('Tsumego');
2856        $this->loadModel('Achievement');
2857        $this->loadModel('AchievementStatus');
2858        $this->loadModel('AchievementCondition');
2859
2860        //$tNum = count($this->Tsumego->find('all', array('conditions' => array('set_id' => $sid))));
2861        $tNum = count(TsumegoUtil::collectTsumegosFromSet($sid));
2862        $s = $this->Set->findById($sid);
2863        $acA = ClassRegistry::init('AchievementCondition')->find('first', [
2864            'order' => 'value DESC',
2865            'conditions' => [
2866                'set_id' => $sid,
2867                'user_id' => Auth::getUserID(),
2868                'category' => '%',
2869            ],
2870        ]);
2871        if (!$acA) {
2872            return [];
2873        }
2874        $acS = ClassRegistry::init('AchievementCondition')->find('first', [
2875            'order' => 'value ASC',
2876            'conditions' => [
2877                'set_id' => $sid,
2878                'user_id' => Auth::getUserID(),
2879                'category' => 's',
2880            ],
2881        ]);
2882        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
2883        if (!$buffer) {
2884            $buffer = [];
2885        }
2886        $existingAs = [];
2887        $bufferCount = count($buffer);
2888        for ($i = 0; $i < $bufferCount; $i++) {
2889            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
2890        }
2891        $as = [];
2892        $as['AchievementStatus']['user_id'] = Auth::getUserID();
2893        $updated = [];
2894
2895        $achievementId = 99;
2896        if ($sid == -1 && !isset($existingAs[$achievementId])) {
2897            $as['AchievementStatus']['achievement_id'] = $achievementId;
2898            ClassRegistry::init('AchievementStatus')->create();
2899            ClassRegistry::init('AchievementStatus')->save($as);
2900            array_push($updated, $achievementId);
2901        }
2902        if ($tNum >= 100) {
2903            if ($s['Set']['difficulty'] < 1300) {
2904                $achievementId = 12;
2905                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
2906                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2907                    ClassRegistry::init('AchievementStatus')->create();
2908                    ClassRegistry::init('AchievementStatus')->save($as);
2909                    array_push($updated, $achievementId);
2910                }
2911                $achievementId = 13;
2912                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
2913                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2914                    ClassRegistry::init('AchievementStatus')->create();
2915                    ClassRegistry::init('AchievementStatus')->save($as);
2916                    array_push($updated, $achievementId);
2917                }
2918                $achievementId = 14;
2919                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
2920                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2921                    ClassRegistry::init('AchievementStatus')->create();
2922                    ClassRegistry::init('AchievementStatus')->save($as);
2923                    array_push($updated, $achievementId);
2924                }
2925                $achievementId = 24;
2926                if ($acS['AchievementCondition']['value'] < 15 && !isset($existingAs[$achievementId])) {
2927                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2928                    ClassRegistry::init('AchievementStatus')->create();
2929                    ClassRegistry::init('AchievementStatus')->save($as);
2930                    array_push($updated, $achievementId);
2931                }
2932                $achievementId = 25;
2933                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
2934                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2935                    ClassRegistry::init('AchievementStatus')->create();
2936                    ClassRegistry::init('AchievementStatus')->save($as);
2937                    array_push($updated, $achievementId);
2938                }
2939                $achievementId = 26;
2940                if ($acS['AchievementCondition']['value'] < 5 && !isset($existingAs[$achievementId])) {
2941                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2942                    ClassRegistry::init('AchievementStatus')->create();
2943                    ClassRegistry::init('AchievementStatus')->save($as);
2944                    array_push($updated, $achievementId);
2945                }
2946            } elseif ($s['Set']['difficulty'] >= 1300 && $s['Set']['difficulty'] < 1500) {
2947                $achievementId = 15;
2948                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
2949                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2950                    ClassRegistry::init('AchievementStatus')->create();
2951                    ClassRegistry::init('AchievementStatus')->save($as);
2952                    array_push($updated, $achievementId);
2953                }
2954                $achievementId = 16;
2955                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
2956                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2957                    ClassRegistry::init('AchievementStatus')->create();
2958                    ClassRegistry::init('AchievementStatus')->save($as);
2959                    array_push($updated, $achievementId);
2960                }
2961                $achievementId = 17;
2962                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
2963                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2964                    ClassRegistry::init('AchievementStatus')->create();
2965                    ClassRegistry::init('AchievementStatus')->save($as);
2966                    array_push($updated, $achievementId);
2967                }
2968                $achievementId = 27;
2969                if ($acS['AchievementCondition']['value'] < 18 && !isset($existingAs[$achievementId])) {
2970                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2971                    ClassRegistry::init('AchievementStatus')->create();
2972                    ClassRegistry::init('AchievementStatus')->save($as);
2973                    array_push($updated, $achievementId);
2974                }
2975                $achievementId = 28;
2976                if ($acS['AchievementCondition']['value'] < 13 && !isset($existingAs[$achievementId])) {
2977                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2978                    ClassRegistry::init('AchievementStatus')->create();
2979                    ClassRegistry::init('AchievementStatus')->save($as);
2980                    array_push($updated, $achievementId);
2981                }
2982                $achievementId = 29;
2983                if ($acS['AchievementCondition']['value'] < 8 && !isset($existingAs[$achievementId])) {
2984                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2985                    ClassRegistry::init('AchievementStatus')->create();
2986                    ClassRegistry::init('AchievementStatus')->save($as);
2987                    array_push($updated, $achievementId);
2988                }
2989            } elseif ($s['Set']['difficulty'] >= 1500 && $s['Set']['difficulty'] < 1700) {
2990                $achievementId = 18;
2991                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
2992                    $as['AchievementStatus']['achievement_id'] = $achievementId;
2993                    ClassRegistry::init('AchievementStatus')->create();
2994                    ClassRegistry::init('AchievementStatus')->save($as);
2995                    array_push($updated, $achievementId);
2996                }
2997                $achievementId = 19;
2998                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
2999                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3000                    ClassRegistry::init('AchievementStatus')->create();
3001                    ClassRegistry::init('AchievementStatus')->save($as);
3002                    array_push($updated, $achievementId);
3003                }
3004                $achievementId = 20;
3005                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
3006                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3007                    ClassRegistry::init('AchievementStatus')->create();
3008                    ClassRegistry::init('AchievementStatus')->save($as);
3009                    array_push($updated, $achievementId);
3010                }
3011                $achievementId = 30;
3012                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
3013                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3014                    ClassRegistry::init('AchievementStatus')->create();
3015                    ClassRegistry::init('AchievementStatus')->save($as);
3016                    array_push($updated, $achievementId);
3017                }
3018                $achievementId = 31;
3019                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
3020                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3021                    ClassRegistry::init('AchievementStatus')->create();
3022                    ClassRegistry::init('AchievementStatus')->save($as);
3023                    array_push($updated, $achievementId);
3024                }
3025                $achievementId = 32;
3026                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
3027                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3028                    ClassRegistry::init('AchievementStatus')->create();
3029                    ClassRegistry::init('AchievementStatus')->save($as);
3030                    array_push($updated, $achievementId);
3031                }
3032            } else {
3033                $achievementId = 21;
3034                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
3035                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3036                    ClassRegistry::init('AchievementStatus')->create();
3037                    ClassRegistry::init('AchievementStatus')->save($as);
3038                    array_push($updated, $achievementId);
3039                }
3040                $achievementId = 22;
3041                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
3042                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3043                    ClassRegistry::init('AchievementStatus')->create();
3044                    ClassRegistry::init('AchievementStatus')->save($as);
3045                    array_push($updated, $achievementId);
3046                }
3047                $achievementId = 23;
3048                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
3049                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3050                    ClassRegistry::init('AchievementStatus')->create();
3051                    ClassRegistry::init('AchievementStatus')->save($as);
3052                    array_push($updated, $achievementId);
3053                }
3054                $achievementId = 33;
3055                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
3056                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3057                    ClassRegistry::init('AchievementStatus')->create();
3058                    ClassRegistry::init('AchievementStatus')->save($as);
3059                    array_push($updated, $achievementId);
3060                }
3061                $achievementId = 34;
3062                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
3063                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3064                    ClassRegistry::init('AchievementStatus')->create();
3065                    ClassRegistry::init('AchievementStatus')->save($as);
3066                    array_push($updated, $achievementId);
3067                }
3068                $achievementId = 35;
3069                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
3070                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3071                    ClassRegistry::init('AchievementStatus')->create();
3072                    ClassRegistry::init('AchievementStatus')->save($as);
3073                    array_push($updated, $achievementId);
3074                }
3075            }
3076            $achievementId = 46;
3077            if ($acA['AchievementCondition']['value'] >= 100) {
3078                $ac100 = ClassRegistry::init('AchievementCondition')->find('all', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => '%', 'value >=' => 100]]);
3079                if (!$ac100) {
3080                    $ac100 = [];
3081                }
3082                $ac100counter = 0;
3083                $ac100Count = count($ac100);
3084                for ($j = 0; $j < $ac100Count; $j++) {
3085                    if (count(TsumegoUtil::collectTsumegosFromSet($ac100[$j]['AchievementCondition']['set_id'])) >= 100) {
3086                        $ac100counter++;
3087                    }
3088                }
3089                $as100 = ClassRegistry::init('AchievementStatus')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'achievement_id' => $achievementId]]);
3090                if ($as100 == null) {
3091                    $as['AchievementStatus']['achievement_id'] = $achievementId;
3092                    $as['AchievementStatus']['value'] = 1;
3093                    ClassRegistry::init('AchievementStatus')->create();
3094                    ClassRegistry::init('AchievementStatus')->save($as);
3095                    array_push($updated, $achievementId);
3096                } elseif ($as100['AchievementStatus']['value'] != $ac100counter) {
3097                    $as100['AchievementStatus']['value'] = $ac100counter;
3098                    ClassRegistry::init('AchievementStatus')->save($as100);
3099                    array_push($updated, $achievementId);
3100                }
3101            }
3102        }
3103        $updatedCount = count($updated);
3104        for ($i = 0; $i < $updatedCount; $i++) {
3105            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
3106            $updated[$i] = [];
3107            $updated[$i][0] = $a['Achievement']['name'];
3108            $updated[$i][1] = $a['Achievement']['description'];
3109            $updated[$i][2] = $a['Achievement']['image'];
3110            $updated[$i][3] = $a['Achievement']['color'];
3111            $updated[$i][4] = $a['Achievement']['xp'];
3112            $updated[$i][5] = $a['Achievement']['id'];
3113        }
3114
3115        return $updated;
3116    }
3117
3118    public static function getXPJump($lvl = null): int {
3119        if ($lvl >= 102) {
3120            return 0;
3121        }
3122        if ($lvl == 101) {
3123            return 1150;
3124        }
3125        if ($lvl == 100) {
3126            return 50000;
3127        }
3128        if ($lvl >= 70) {
3129            return 150;
3130        }
3131        if ($lvl >= 40) {
3132            return 100;
3133        }
3134        if ($lvl >= 20) {
3135            return 50;
3136        }
3137        if ($lvl >= 12) {
3138            return 25;
3139        }
3140
3141        return 10;
3142    }
3143
3144    public static function getHealth($lvl = null): int {
3145        if ($lvl >= 100) {
3146            return 30;
3147        }
3148        if ($lvl >= 95) {
3149            return 29;
3150        }
3151        if ($lvl >= 90) {
3152            return 28;
3153        }
3154        if ($lvl >= 85) {
3155            return 27;
3156        }
3157        if ($lvl >= 80) {
3158            return 26;
3159        }
3160        if ($lvl >= 75) {
3161            return 25;
3162        }
3163        if ($lvl >= 70) {
3164            return 24;
3165        }
3166        if ($lvl >= 65) {
3167            return 23;
3168        }
3169        if ($lvl >= 60) {
3170            return 22;
3171        }
3172        if ($lvl >= 55) {
3173            return 21;
3174        }
3175        if ($lvl >= 50) {
3176            return 20;
3177        }
3178        if ($lvl >= 45) {
3179            return 19;
3180        }
3181        if ($lvl >= 40) {
3182            return 18;
3183        }
3184        if ($lvl >= 35) {
3185            return 17;
3186        }
3187        if ($lvl >= 30) {
3188            return 16;
3189        }
3190        if ($lvl >= 25) {
3191            return 15;
3192        }
3193        if ($lvl >= 20) {
3194            return 14;
3195        }
3196        if ($lvl >= 15) {
3197            return 13;
3198        }
3199        if ($lvl >= 10) {
3200            return 12;
3201        }
3202        if ($lvl >= 4) {
3203            return 11;
3204        }
3205
3206        return 10;
3207    }
3208
3209    public static function updateXP($userID, $achievementData): void {
3210        $xpBonus = 0;
3211        $aCount = count($achievementData);
3212        for ($i = 0; $i < $aCount; $i++) {
3213            $xpBonus += $achievementData[$i][4];
3214        }
3215        $u = ClassRegistry::init('User')->findById($userID);
3216        $jumps = [];
3217        $xStart = 40;
3218
3219        for ($i = 1; $i < 102; $i++) {
3220            if ($i == 101) {
3221                $j = 1150;
3222            } elseif ($i == 100) {
3223                $j = 50000;
3224            } elseif ($i >= 70) {
3225                $j = 150;
3226            } elseif ($i >= 40) {
3227                $j = 100;
3228            } elseif ($i >= 20) {
3229                $j = 50;
3230            } elseif ($i >= 12) {
3231                $j = 25;
3232            } else {
3233                $j = 10;
3234            }
3235            $xStart += $j;
3236            $jumps[$i] = $xStart;
3237        }
3238        $next = 0;
3239        $uLevel = $u['User']['level'];
3240        $uXp = $u['User']['xp'];
3241
3242        if ($uLevel < 101) {
3243            $currentJump = $jumps[$uLevel];
3244        } else {
3245            $currentJump = 60000;
3246        }
3247
3248        $firstJump = $currentJump;
3249
3250        if ($uXp + $xpBonus >= $currentJump) {
3251            $next = $uXp + $xpBonus - $currentJump;
3252            $uLevel++;
3253            if ($uLevel < 101) {
3254                $currentJump = $jumps[$uLevel];
3255            } else {
3256                $currentJump = 60000;
3257            }
3258        }
3259        while ($next >= $currentJump) {
3260            $next = $next - $currentJump;
3261            $uLevel++;
3262            if ($uLevel < 101) {
3263                $currentJump = $jumps[$uLevel];
3264            } else {
3265                $currentJump = 60000;
3266            }
3267        }
3268
3269        $u['User']['level'] = $uLevel;
3270        if ($uXp + $xpBonus < $firstJump) {
3271            $u['User']['xp'] += $xpBonus;
3272        } else {
3273            $u['User']['xp'] = $next;
3274        }
3275        $u['User']['nextlvl'] = $currentJump;
3276
3277        ClassRegistry::init('User')->save($u);
3278    }
3279
3280    public static function getPartitionRange($amountRemaining, $collectionSize, $partition) {
3281        if ($collectionSize > 0) {
3282            $amountPartitions = ceil($amountRemaining / $collectionSize);
3283        } else {
3284            return ['0', $collectionSize - 1];
3285        }
3286        if ($collectionSize > 0 && $amountRemaining % $collectionSize == 0) {
3287            return [ $partition * $collectionSize,  $partition * $collectionSize + $collectionSize - 1];
3288        }
3289        $amountCounter = 0;
3290        $amountFrom = 0;
3291        $amountTo = $collectionSize - 1;
3292        while ($amountRemaining > $collectionSize) {
3293            if ($partition == $amountCounter) {
3294                break;
3295            }
3296            $amountRemaining -= $collectionSize;
3297            $amountCounter++;
3298            $amountFrom += $collectionSize;
3299            $amountTo += $collectionSize;
3300        }
3301        $amountTo = $amountFrom + $collectionSize - 1;
3302        if ($partition >= $amountPartitions - 1) {
3303            $amountTo = $amountFrom + $amountRemaining - 1;
3304        }
3305        $a = [];
3306        $a[0] = $amountFrom;
3307        $a[1] = $amountTo;
3308
3309        return $a;
3310    }
3311
3312    /**
3313     * @param int $uid User ID
3314     * @return void
3315     */
3316    protected function handleSearchSettings($uid) {
3317        $this->loadModel('UserContribution');
3318        $uc = $this->UserContribution->find('first', ['conditions' => ['user_id' => $uid]]);
3319        if ($uc == null) {
3320            $uc = [];
3321            $uc['UserContribution']['user_id'] = $uid;
3322            $uc['UserContribution']['added_tag'] = 0;
3323            $uc['UserContribution']['created_tag'] = 0;
3324            $uc['UserContribution']['made_proposal'] = 0;
3325            $uc['UserContribution']['reviewed'] = 0;
3326            $uc['UserContribution']['score'] = 0;
3327            $this->UserContribution->create();
3328            $this->UserContribution->save($uc);
3329        }
3330        new TsumegoFilters();
3331    }
3332
3333    protected function signIn(array $user): void {
3334        Auth::init($user);
3335        $vs = $this->TsumegoStatus->find('first', ['conditions' => ['user_id' => $user['User']['id']], 'order' => 'created DESC']);
3336        if ($vs) {
3337            $this->Session->write('lastVisit', $vs['TsumegoStatus']['tsumego_id']);
3338        }
3339        $this->Session->write('texture', $user['User']['texture']);
3340        $this->Session->write('check1', $user['User']['id']);
3341    }
3342
3343    public function beforeFilter(): void {
3344        $this->loadModel('User');
3345        $this->loadModel('Activate');
3346        $this->loadModel('Tsumego');
3347        $this->loadModel('TsumegoRatingAttempt');
3348        $this->loadModel('Set');
3349        $this->loadModel('TimeModeAttempt');
3350        $this->loadModel('TsumegoStatus');
3351        $this->loadModel('Comment');
3352        $this->loadModel('UserBoard');
3353        $this->loadModel('TsumegoAttempt');
3354        $this->loadModel('AdminActivity');
3355        $this->loadModel('Achievement');
3356        $this->loadModel('AchievementStatus');
3357        $this->loadModel('AchievementCondition');
3358        $this->loadModel('SetConnection');
3359        $this->loadModel('Tag');
3360        $this->loadModel('TagName');
3361        $this->loadModel('Favorite');
3362
3363        Auth::init();
3364        $this->TimeMode->init();
3365
3366        $highscoreLink = 'highscore';
3367        $lightDark = 'light';
3368        $resetCookies = false;
3369        $levelBar = 1;
3370        $lastProfileLeft = 1;
3371        $lastProfileRight = 2;
3372        $hasFavs = false;
3373
3374        if (Auth::isLoggedIn()) {
3375            if (isset($_COOKIE['addTag']) && $_COOKIE['addTag'] != 0 && $this->Session->read('page') != 'set') {
3376                $newAddTag = explode('-', $_COOKIE['addTag']);
3377                $tagId = $newAddTag[0];
3378                $newTagName = $this->TagName->find('first', ['conditions' => ['name' => str_replace($tagId . '-', '', $_COOKIE['addTag'])]]);
3379                if ($newTagName) {
3380                    $saveTag = [];
3381                    $saveTag['Tag']['tag_name_id'] = $newTagName['TagName']['id'];
3382                    $saveTag['Tag']['tsumego_id'] = $tagId;
3383                    $saveTag['Tag']['user_id'] = Auth::getUserID();
3384                    $saveTag['Tag']['approved'] = 0;
3385                    $this->Tag->save($saveTag);
3386                }
3387                $this->set('removeCookie', 'addTag');
3388            }
3389            if (isset($_COOKIE['z_sess']) && $_COOKIE['z_sess'] != 0
3390            && strlen($_COOKIE['z_sess']) > 5) {
3391                Auth::getUser()['_sessid'] = $_COOKIE['z_sess'];
3392                Auth::saveUser();
3393            }
3394            if (Auth::getUser()['lastHighscore'] == 1) {
3395                $highscoreLink = 'highscore';
3396            } elseif (Auth::getUser()['lastHighscore'] == 2) {
3397                $highscoreLink = 'rating';
3398            } elseif (Auth::getUser()['lastHighscore'] == 3) {
3399                $highscoreLink = 'leaderboard';
3400            } elseif (Auth::getUser()['lastHighscore'] == 4) {
3401                $highscoreLink = 'highscore3';
3402            }
3403
3404            if (isset($_COOKIE['lastMode']) && $_COOKIE['lastMode'] != 0) {
3405                Auth::getUser()['lastMode'] = $_COOKIE['lastMode'];
3406                Auth::saveUser();
3407            }
3408            if (isset($_COOKIE['sound']) && $_COOKIE['sound'] != '0') {
3409                Auth::getUser()['sound'] = $_COOKIE['sound'];
3410                Auth::saveUser();
3411                unset($_COOKIE['sound']);
3412            }
3413            $this->set('ac', true);
3414            $this->set('user', Auth::getUser());
3415        }
3416
3417        if (isset($_COOKIE['lightDark']) && $_COOKIE['lightDark'] != '0') {
3418            $lightDark = $_COOKIE['lightDark'];
3419            if (Auth::isLoggedIn()) {
3420                // Convert string to integer for database storage
3421                $lightDarkInt = ($lightDark === 'light') ? 0 : 2;
3422                Auth::getUser()['lastLight'] = $lightDarkInt;
3423            }
3424        } elseif (Auth::isLoggedIn()) {
3425            if (Auth::getUser()['lastLight'] == 0
3426            || Auth::getUser()['lastLight'] == 1) {
3427                $lightDark = 'light';
3428            } else {
3429                $lightDark = 'dark';
3430            }
3431        }
3432
3433        if (Auth::isLoggedIn()) {
3434            $this->handleSearchSettings(Auth::getUserID());
3435            if (isset($_COOKIE['levelBar']) && $_COOKIE['levelBar'] != '0') {
3436                $levelBar = $_COOKIE['levelBar'];
3437                Auth::getUser()['levelBar'] = $levelBar;
3438            } elseif (Auth::getUser()['levelBar'] == 0
3439          || Auth::getUser()['levelBar'] == 'level') {
3440                $levelBar = 1;
3441            } else {
3442                $levelBar = 2;
3443            }
3444
3445            if (isset($_COOKIE['lastProfileLeft']) && $_COOKIE['lastProfileLeft'] != '0') {
3446                $lastProfileLeft = $_COOKIE['lastProfileLeft'];
3447                Auth::getUser()['lastProfileLeft'] = $lastProfileLeft;
3448            } else {
3449                $lastProfileLeft = Auth::getUser()['lastProfileLeft'];
3450                if ($lastProfileLeft == 0) {
3451                    $lastProfileLeft = 1;
3452                }
3453            }
3454            if (isset($_COOKIE['lastProfileRight']) && $_COOKIE['lastProfileRight'] != '0') {
3455                $lastProfileRight = $_COOKIE['lastProfileRight'];
3456                Auth::getUser()['lastProfileRight'] = $lastProfileRight;
3457            } else {
3458                $lastProfileRight = Auth::getUser()['lastProfileRight'];
3459                if ($lastProfileRight == 0) {
3460                    $lastProfileRight = 1;
3461                }
3462            }
3463        }
3464        $mode = 1;
3465        if (isset($_COOKIE['mode']) && $_COOKIE['mode'] != '0') {
3466            if ($_COOKIE['mode'] == 1) {
3467                $mode = 1;
3468            } else {
3469                $mode = 2;
3470            }
3471        }
3472
3473        if (Auth::isLoggedIn() && Auth::getUser()['mode'] == 2) {
3474            $mode = 2;
3475        }
3476
3477        if ($_COOKIE['sprint'] != 1) {
3478            $this->updateSprintCondition();
3479        }
3480        $correctSolveAttempt = false;
3481
3482        if (Auth::isLoggedIn()) {
3483            if (isset($_COOKIE['revelation']) && $_COOKIE['revelation'] != 0) {
3484                Auth::getUser()['revelation'] -= 1;
3485            }
3486
3487            if (!$this->request->is('ajax')) {
3488                $this->PlayResultProcessor->checkPreviousPlay($this->TimeMode);
3489            }
3490
3491            if (isset($_COOKIE['noScore']) && isset($_COOKIE['noPreId'])) {
3492                if ($_COOKIE['noScore'] != '0' && $_COOKIE['noPreId'] != '0') {
3493                    //$previosTsumegoX = $this->Tsumego->findById($_COOKIE['noPreId']);
3494                    //$previosTsumegoXsc = $this->SetConnection->find('first', array('conditions' => array('tsumego_id' => $_COOKIE['noPreId'])));
3495                    //$scoreArrX = explode('-', $this->decrypt($_COOKIE['noScore']));
3496
3497                    $utPreX = $this->TsumegoStatus->find('first', ['conditions' => ['tsumego_id' => $_COOKIE['noPreId'], 'user_id' => Auth::getUserID()]]);
3498                    if ($utPreX == null) {
3499                        $utPreX['TsumegoStatus'] = [];
3500                        $utPreX['TsumegoStatus']['user_id'] = Auth::getUserID();
3501                        $utPreX['TsumegoStatus']['tsumego_id'] = $_COOKIE['noPreId'];
3502                    }
3503                    if ($utPreX['TsumegoStatus']['status'] == 'W') {
3504                        $utPreX['TsumegoStatus']['status'] = 'C';
3505                    } else {
3506                        $utPreX['TsumegoStatus']['status'] = 'S';
3507                    }
3508                    $utPreX['TsumegoStatus']['created'] = date('Y-m-d H:i:s');
3509                    //$this->TsumegoStatus->save($utPreX);
3510                    //$sessionUts = $this->Session->read('loggedInUser.uts');
3511                    /*if (!$sessionUts) {
3512                    $sessionUts = [];
3513                    }
3514                    $sessionUts[$utPreX['TsumegoStatus']['tsumego_id']] = $utPreX['TsumegoStatus']['status'];
3515                    $this->Session->write('loggedInUser.uts', $sessionUts);*/
3516                }
3517            }
3518        }
3519        $boardNames = [];
3520        $enabledBoards = [];
3521        $boardPositions = [];
3522
3523        $boardNames[1] = 'Pine';
3524        $boardNames[2] = 'Ash';
3525        $boardNames[3] = 'Maple';
3526        $boardNames[4] = 'Shin Kaya';
3527        $boardNames[5] = 'Birch';
3528        $boardNames[6] = 'Wenge';
3529        $boardNames[7] = 'Walnut';
3530        $boardNames[8] = 'Mahogany';
3531        $boardNames[9] = 'Blackwood';
3532        $boardNames[10] = 'Marble 1';
3533        $boardNames[11] = 'Marble 2';
3534        $boardNames[12] = 'Marble 3';
3535        $boardNames[13] = 'Tibet Spruce';
3536        $boardNames[14] = 'Marble 4';
3537        $boardNames[15] = 'Marble 5';
3538        $boardNames[16] = 'Quarry 1';
3539        $boardNames[17] = 'Flowers';
3540        $boardNames[18] = 'Nova';
3541        $boardNames[19] = 'Spring';
3542        $boardNames[20] = 'Moon';
3543        $boardNames[21] = 'Apex';
3544        $boardNames[22] = 'Gold 1';
3545        $boardNames[23] = 'Amber';
3546        $boardNames[24] = 'Marble 6';
3547        $boardNames[25] = 'Marble 7';
3548        $boardNames[26] = 'Marble 8';
3549        $boardNames[27] = 'Marble 9';
3550        $boardNames[28] = 'Marble 10';
3551        $boardNames[29] = 'Jade';
3552        $boardNames[30] = 'Quarry 2';
3553        $boardNames[31] = 'Black Bricks';
3554        $boardNames[32] = 'Wallpaper 1';
3555        $boardNames[33] = 'Wallpaper 2';
3556        $boardNames[34] = 'Gold & Gray';
3557        $boardNames[35] = 'Gold & Pink';
3558        $boardNames[36] = 'Veil';
3559        $boardNames[37] = 'Tiles';
3560        $boardNames[38] = 'Mars';
3561        $boardNames[39] = 'Pink Cloud';
3562        $boardNames[40] = 'Reptile';
3563        $boardNames[41] = 'Mezmerizing';
3564        $boardNames[42] = 'Magenta Sky';
3565        $boardNames[43] = 'Tsumego Hero';
3566        $boardNames[44] = 'Pretty';
3567        $boardNames[45] = 'Hunting';
3568        $boardNames[46] = 'Haunted';
3569        $boardNames[47] = 'Carnage';
3570        $boardNames[48] = 'Blind Spot';
3571        $boardNames[49] = 'Giants';
3572        $boardNames[50] = 'Gems';
3573        $boardNames[51] = 'Grandmaster';
3574        $boardPositions[1] = [1, 'texture1', 'black34.png', 'white34.png'];
3575        $boardPositions[2] = [2, 'texture2', 'black34.png', 'white34.png'];
3576        $boardPositions[3] = [3, 'texture3', 'black34.png', 'white34.png'];
3577        $boardPositions[4] = [4, 'texture4', 'black.png', 'white.png'];
3578        $boardPositions[5] = [5, 'texture5', 'black34.png', 'white34.png'];
3579        $boardPositions[6] = [6, 'texture6', 'black.png', 'white.png'];
3580        $boardPositions[7] = [7, 'texture7', 'black34.png', 'white34.png'];
3581        $boardPositions[8] = [8, 'texture8', 'black.png', 'white.png'];
3582        $boardPositions[9] = [9, 'texture9', 'black.png', 'white.png'];
3583        $boardPositions[10] = [10, 'texture10', 'black34.png', 'white34.png'];
3584        $boardPositions[11] = [11, 'texture11', 'black34.png', 'white34.png'];
3585        $boardPositions[12] = [12, 'texture12', 'black34.png', 'white34.png'];
3586        $boardPositions[13] = [13, 'texture13', 'black34.png', 'white34.png'];
3587        $boardPositions[14] = [14, 'texture14', 'black34.png', 'white34.png'];
3588        $boardPositions[15] = [15, 'texture15', 'black.png', 'white.png'];
3589        $boardPositions[16] = [16, 'texture16', 'black34.png', 'white34.png'];
3590        $boardPositions[17] = [17, 'texture17', 'black34.png', 'white34.png'];
3591        $boardPositions[18] = [18, 'texture18', 'black.png', 'white.png'];
3592        $boardPositions[19] = [19, 'texture19', 'black34.png', 'white34.png'];
3593        $boardPositions[20] = [20, 'texture20', 'black34.png', 'white34.png'];
3594        $boardPositions[21] = [33, 'texture33', 'black34.png', 'white34.png'];
3595        $boardPositions[22] = [21, 'texture21', 'black.png', 'whiteKo.png'];
3596        $boardPositions[23] = [22, 'texture22', 'black34.png', 'white34.png'];
3597        $boardPositions[24] = [34, 'texture34', 'black.png', 'white.png'];
3598        $boardPositions[25] = [35, 'texture35', 'black34.png', 'white34.png'];
3599        $boardPositions[26] = [36, 'texture36', 'black.png', 'white.png'];
3600        $boardPositions[27] = [37, 'texture37', 'black34.png', 'white34.png'];
3601        $boardPositions[28] = [38, 'texture38', 'black38.png', 'white34.png'];
3602        $boardPositions[29] = [39, 'texture39', 'black.png', 'white.png'];
3603        $boardPositions[30] = [40, 'texture40', 'black34.png', 'white34.png'];
3604        $boardPositions[31] = [41, 'texture41', 'black34.png', 'white34.png'];
3605        $boardPositions[32] = [42, 'texture42', 'black34.png', 'white42.png'];
3606        $boardPositions[33] = [43, 'texture43', 'black34.png', 'white42.png'];
3607        $boardPositions[34] = [44, 'texture44', 'black34.png', 'white34.png'];
3608        $boardPositions[35] = [45, 'texture45', 'black34.png', 'white42.png'];
3609        $boardPositions[36] = [47, 'texture47', 'black34.png', 'white34.png'];
3610        $boardPositions[37] = [48, 'texture48', 'black34.png', 'white34.png'];
3611        $boardPositions[38] = [49, 'texture49', 'black.png', 'white.png'];
3612        $boardPositions[39] = [50, 'texture50', 'black34.png', 'white34.png'];
3613        $boardPositions[40] = [51, 'texture51', 'black34.png', 'white34.png'];
3614        $boardPositions[41] = [52, 'texture52', 'black34.png', 'white34.png'];
3615        $boardPositions[42] = [53, 'texture53', 'black34.png', 'white34.png'];
3616        $boardPositions[43] = [54, 'texture54', 'black54.png', 'white54.png'];
3617        $boardPositions[44] = [23, 'texture23', 'black.png', 'whiteFlower.png'];
3618        $boardPositions[45] = [24, 'texture24', 'black24.png', 'white24.png'];
3619        $boardPositions[46] = [25, 'texture25', 'blackGhost.png', 'white.png'];
3620        $boardPositions[47] = [26, 'texture26', 'blackInvis.png', 'whiteCarnage.png'];
3621        $boardPositions[48] = [27, 'texture27', 'black27.png', 'white27.png'];
3622        $boardPositions[49] = [28, 'texture28', 'blackGiant.png', 'whiteKo.png'];
3623        $boardPositions[50] = [29, 'texture29', 'blackKo.png', 'whiteKo.png'];
3624        $boardPositions[51] = [30, 'texture55', 'blackGalaxy.png', 'whiteGalaxy.png'];
3625
3626        $boardCount = 51;
3627
3628        if ($this->Session->check('texture') || (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0')) {
3629            $splitCookie = [];
3630            if (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0') {
3631                $splitCookie = str_split($_COOKIE['texture']);
3632                $textureCookies = $_COOKIE['texture'];
3633                Auth::getUser()['texture'] = $this->Session->read('texture');
3634                $this->Session->write('texture', $_COOKIE['texture']);
3635                $this->set('textureCookies', $textureCookies);
3636            } else {
3637                if (Auth::isLoggedIn()) {
3638                    $this->Session->write('texture', Auth::getUser()['texture']);
3639                }
3640                $textureCookies = $this->Session->read('texture');
3641                $splitTextureCookies = str_split($textureCookies);
3642                $splitTextureCookiesCount = count($splitTextureCookies);
3643                for ($i = 0; $i < $splitTextureCookiesCount; $i++) {
3644                    if ($splitTextureCookies[$i] == 2) {
3645                        $enabledBoards[$i + 1] = 'checked';
3646                    } else {
3647                        $enabledBoards[$i + 1] = '';
3648                    }
3649                }
3650            }
3651
3652            $splitCookieCount = count($splitCookie);
3653            for ($i = 0; $i < $splitCookieCount; $i++) {
3654                if ($splitCookie[$i] == 2) {
3655                    $enabledBoards[$i + 1] = 'checked';
3656                } else {
3657                    $enabledBoards[$i + 1] = '';
3658                }
3659            }
3660            if (Auth::isLoggedIn()) {
3661                Auth::saveUser();
3662            }
3663        }
3664
3665        if (!$this->Session->check('texture')) {
3666            $this->Session->write('texture', '222222221111111111111111111111111111111111111111111');
3667            $enabledBoards[1] = 'checked';
3668            $enabledBoards[2] = 'checked';
3669            $enabledBoards[3] = 'checked';
3670            $enabledBoards[4] = 'checked';
3671            $enabledBoards[5] = 'checked';
3672            $enabledBoards[6] = 'checked';
3673            $enabledBoards[7] = 'checked';
3674            $enabledBoards[8] = 'checked';
3675            $enabledBoards[9] = '';
3676            $enabledBoards[10] = '';
3677            $enabledBoards[11] = '';
3678            $enabledBoards[12] = '';
3679            $enabledBoards[13] = '';
3680            $enabledBoards[14] = '';
3681            $enabledBoards[15] = '';
3682            $enabledBoards[16] = '';
3683            $enabledBoards[17] = '';
3684            $enabledBoards[18] = '';
3685            $enabledBoards[19] = '';
3686            $enabledBoards[20] = '';
3687            $enabledBoards[21] = '';
3688            $enabledBoards[22] = '';
3689            $enabledBoards[23] = '';
3690            $enabledBoards[24] = '';
3691            $enabledBoards[25] = '';
3692            $enabledBoards[26] = '';
3693            $enabledBoards[27] = '';
3694            $enabledBoards[28] = '';
3695            $enabledBoards[29] = '';
3696            $enabledBoards[30] = '';
3697            $enabledBoards[31] = '';
3698            $enabledBoards[32] = '';
3699            $enabledBoards[33] = '';
3700            $enabledBoards[34] = '';
3701            $enabledBoards[35] = '';
3702            $enabledBoards[36] = '';
3703            $enabledBoards[37] = '';
3704            $enabledBoards[38] = '';
3705            $enabledBoards[39] = '';
3706            $enabledBoards[40] = '';
3707            $enabledBoards[41] = '';
3708            $enabledBoards[42] = '';
3709            $enabledBoards[43] = '';
3710            $enabledBoards[44] = '';
3711            $enabledBoards[45] = '';
3712            $enabledBoards[46] = '';
3713            $enabledBoards[47] = '';
3714            $enabledBoards[48] = '';
3715            $enabledBoards[49] = '';
3716            $enabledBoards[50] = '';
3717            $enabledBoards[51] = '';
3718        }
3719        $achievementUpdate = [];
3720        if ($this->Session->check('initialLoading')) {
3721            $achievementUpdate1 = $this->checkLevelAchievements();
3722            $achievementUpdate2 = $this->checkProblemNumberAchievements();
3723            $achievementUpdate3 = $this->checkRatingAchievements();
3724            $achievementUpdate4 = $this->checkTimeModeAchievements();
3725            $achievementUpdate5 = $this->checkDanSolveAchievements();
3726            $achievementUpdate = array_merge(
3727                $achievementUpdate1 ?: [],
3728                $achievementUpdate2 ?: [],
3729                $achievementUpdate3 ?: [],
3730                $achievementUpdate4 ?: [],
3731                $achievementUpdate5 ?: []
3732            );
3733            $this->Session->delete('initialLoading');
3734        }
3735
3736        if (count($achievementUpdate) > 0) {
3737            $this->updateXP(Auth::getUserID(), $achievementUpdate);
3738        }
3739
3740        $nextDay = new DateTime('tomorrow');
3741        if (Auth::isLoggedIn()) {
3742            Auth::getUser()['name'] = $this->checkPicture(Auth::getUser());
3743            $this->set('user', Auth::getUser());
3744        }
3745        $this->set('mode', $mode);
3746        $this->set('nextDay', $nextDay->format('m/d/Y'));
3747        $this->set('boardNames', $boardNames);
3748        $this->set('enabledBoards', $enabledBoards);
3749        $this->set('boardPositions', $boardPositions);
3750        $this->set('highscoreLink', $highscoreLink);
3751        $this->set('achievementUpdate', $achievementUpdate);
3752        $this->set('lightDark', $lightDark);
3753        $this->set('levelBar', $levelBar);
3754        $this->set('lastProfileLeft', $lastProfileLeft);
3755        $this->set('lastProfileRight', $lastProfileRight);
3756        $this->set('resetCookies', $resetCookies);
3757        $this->set('hasFavs', $hasFavs);
3758    }
3759
3760    public function afterFilter() {}
3761}